【发布时间】:2014-06-09 10:05:30
【问题描述】:
我正在尝试构建一个特征来检查是否存在嵌套的模板类。这是我检查一个类O是否有一个嵌套类inner和模板参数T:
template <typename O, typename T> struct has_inner {
static const bool value = std::is_class<typename O::template inner<T> >::value;
};
但是,这不能正常工作。给定两个示例类dummy 和ok
struct dummy {};
struct ok {
template <typename T>
struct inner {
};
};
检查ok
std::cout << std::boolalpha << has_inner<ok, float>::value << std::endl;
会起作用,而检查dummy
std::cout << std::boolalpha << has_inner<dummy, int>::value << std::endl;
将无法在 clang 3.2 上编译并出现错误
error: 'inner' following the 'template' keyword does not refer to a template
static const bool value = std::is_class<typename O::template inner<T> >::value;
^~~~~
note: in instantiation of template class 'has_inner<dummy, int>' requested here
std::cout << std::boolalpha << has_inner<dummy, int>::value << std::endl;
编译器似乎在将模板化表达式传递给std::is_class 之前尝试实际形成该表达式。因此,我看到了两种解决方案:
- 告诉编译器延迟模板扩展,或者
- 完全使用不同的方法。
但是,我也不知道该怎么做,谁能帮忙?
【问题讨论】:
标签: c++ templates c++11 typetraits