c++ - 没有 Boost 的 BOOST_DEDUCED_TYPENAME 的实现

标签 c++ c++11 boost metaprogramming template-meta-programming

有以下代码片段:

template<typename ValueType>
ValueType any_cast(any & operand)
{
    typedef BOOST_DEDUCED_TYPENAME remove_reference<ValueType>::type nonref;


    nonref * result = any_cast<nonref>(&operand);
    if(!result)
        boost::throw_exception(bad_any_cast());

    // Attempt to avoid construction of a temporary object in cases when 
    // `ValueType` is not a reference. Example:
    // `static_cast<std::string>(*result);` 
    // which is equal to `std::string(*result);`
    typedef BOOST_DEDUCED_TYPENAME boost::mpl::if_<
        boost::is_reference<ValueType>,
        ValueType,
        BOOST_DEDUCED_TYPENAME boost::add_reference<ValueType>::type
    >::type ref_type;

    return static_cast<ref_type>(*result);
}

是否可以在没有 Boost 的情况下实现 BOOST_DEDUCED_TYPENAME?我只能使用 C++11

最佳答案

如果您有支持 C++11 的编译器,我发现您不太可能需要 BOOST_DEDUCED_TYPENAME。即,它位于 include/boost/config/suffix.hpp 中:

// BOOST_DEDUCED_TYPENAME workaround ------------------------------------------//
//
// Some compilers don't support the use of `typename' for dependent
// types in deduced contexts, e.g.
//
//     template <class T> void f(T, typename T::type);
//                                  ^^^^^^^^
// Replace these declarations with:
//
//     template <class T> void f(T, BOOST_DEDUCED_TYPENAME T::type);

#ifndef BOOST_NO_DEDUCED_TYPENAME
#  define BOOST_DEDUCED_TYPENAME typename
#else
#  define BOOST_DEDUCED_TYPENAME
#endif

只需使用typename关键字即可。

关于c++ - 没有 Boost 的 BOOST_DEDUCED_TYPENAME 的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27465106/

相关文章:

c++ - fstream seekg()、seekp() 和 write()

c++ - 递归 C++ 阶乘为多个条目提供负值

c++ - 为什么将表达式转换为右值对函数的引用是左值?

c++ - qi::rule<It, std::string ()> 不解析输入字符串

c++ - shared_from_this() 返回 std::shared_ptr<const X>,而不是 std::shared_ptr<X>

const 类型的构造函数初始值设定项的 C++ 正确输入验证

C++ lambda表达式类中的变量

c++ - 我的等待 - 使用 std::mutex 的通知机制是否正确?

c++ - Eclipse 上的 Autotools 项目和外部库

c++ - 我什么时候必须使用 boost::asio:strand