Den*_*nis 2 c++ boost metaprogramming template-meta-programming c++11
有以下代码段:
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);
}
Run Code Online (Sandbox Code Playgroud)
是否有可能实现BOOST_DEDUCED_TYPENAME不Boost?我只能使用C++11.
小智 5
如果你有一个支持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
Run Code Online (Sandbox Code Playgroud)
只需使用typename关键字.