【发布时间】:2017-10-08 23:03:46
【问题描述】:
我正在尝试实现一种机制来检测提供的类是否包含一些静态方法。这是非常简单的代码,但我不明白为什么 decltype() 对于 EnableIfHasFooMethod 类的专业化不能按预期工作:
#include <iostream>
struct A {
static int Foo() { return 0; }
};
template <class T, class = void>
struct EnableIfHasFooMethod {};
template <class T>
struct EnableIfHasFooMethod<T, decltype(T::Foo)> {
typedef void type;
};
template <class T, class = void>
struct HasFooMethod {
static const bool value = false;
};
template <class T>
struct HasFooMethod<T, typename EnableIfHasFooMethod<T>::type> {
static const bool value = true;
};
int main() {
std::cout << HasFooMethod<A>::value << std::endl;
return 0;
}
输出是0,但应该是1。
【问题讨论】:
标签: c++ c++11 templates template-specialization sfinae