有以下代码片段:
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/