c++ - boost::any 构造函数 - const 类型重载解析

标签 c++ boost boost-any

boost::any 有一个完美的前向构造函数声明为:

template<typename ValueType>
any(ValueType&& value
    , typename boost::disable_if<boost::is_same<any&, ValueType> >::type* = 0 // disable if value has type `any&`
    , typename boost::disable_if<boost::is_const<ValueType> >::type* = 0) // disable if value has type `const ValueType&&`
  : content(new holder< typename decay<ValueType>::type >(static_cast<ValueType&&>(value)))
{}

is_const<> SFINAE 排除将 const 类型强制为常规复制构造函数:

template<typename ValueType>
any(const ValueType & value)
  : content(new holder<
        BOOST_DEDUCED_TYPENAME remove_cv<BOOST_DEDUCED_TYPENAME decay<const ValueType>::type>::type
    >(value))
{}

如果移除 is_const<> 排除项,常规复制构造函数处理 const 值的方式与完美正向构造函数的处理方式有何不同?

最佳答案

第一个专门用于右值引用(enable_if 确保它)。

static_cast 是使用 std::move 的同义词,所以它会移动。

关于c++ - boost::any 构造函数 - const 类型重载解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35419402/

相关文章:

c++ - Win32 API 按钮看起来很新,但它使用旧字体

c++ - 将 std::list 扩展为循环列表

c++ - Boost.Any 与 Boost.Variant

C++从图中删除顶点

c++ - 在 beast 1.70.0 中使用 Tcp 超时

c++ - boost::any_cast 到我的自定义类

c++ - boost::any 需要 RTTI 吗?

C++ Ogre,不会编译和运行

C++ 命名空间和语法头文件错误

c++ - boost::spirit::气