boost::any 中的模板复制构造函数
我对 boost 的 any.hpp 中的这些代码感到困惑。
template<typename ValueType>
any(const ValueType & value)
: content(new holder<
BOOST_DEDUCED_TYPENAME remove_cv<BOOST_DEDUCED_TYPENAME decay<const ValueType>::type>::type
>(value))
{
}
any(const any & other)
: content(other.content ? other.content->clone() : 0)
{
}
很明显,当我需要来自另一个对象的新对象时,sencod 复制构造函数很有用。 但是第一个copy-constructed什么时候执行呢?
最佳答案
模板构造函数(不是复制构造函数)从对 ValueType
的某个对象的 const 引用构造一个 boost::any
。复制构造函数制作 any 的拷贝(对其中的对象执行多态克隆)。
这是第一个表单何时被选中的示例:
std::string s = "Hello, World";
boost::any a(s); // template constructor selected here
boost::any b(a); // copy constructor selected here.
关于c++ - boost::any 中的模板复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33667870/