我目前正在做一些模板元编程。就我而言,我可以处理任何“可迭代”类型,即任何类型typedef foo const_iterator
以同样的方式存在。我试图为此使用新的 C++11 模板元编程,但是我找不到检测某种类型是否丢失的方法。
因为我还需要根据其他特征打开/关闭其他模板专业化,所以我当前使用带有两个参数的模板,第二个参数是通过生成的std::enable_if
。这是我目前正在做的事情:
template <typename T, typename Enable = void>
struct Foo{}; // default case is invalid
template <typename T>
struct Foo< T, typename std::enable_if<std::is_fundamental<T>::value>::type>{
void do_stuff(){ ... }
};
template<typename T>
struct exists{
static const bool value = true;
};
template<typename T>
struct Foo<T, typename std::enable_if<exists< typename T::const_iterator >::value >::type> {
void do_stuff(){ ... }
};
如果没有的话我就无法做这样的事情exists
助手模板。例如简单地做
template<typename T>
struct Foo<T, typename T::const_iterator> {
void do_stuff(){ ... }
};
不起作用,因为在应该使用此专门化的情况下,会实例化无效的默认情况。
但是我找不到这个exists
新的 C++11 标准中的任何地方,据我所知,它只是取自boost::type_traits
对于这种东西。然而关于homepage http://www.boost.org/doc/libs/1_47_0/libs/type_traits/doc/html/index.html for boost::type_traits
没有显示任何可以用来替代的东西的参考。
是否缺少此功能,或者我是否忽略了其他一些明显的方法来实现所需的行为?