제공된 클래스에 정적 메서드가 있는지 여부를 검색하는 메커니즘을 구현하려고합니다. 그것은 아주 간단한 코드입니다하지만 난 EnableIfHasFooMethod
클래스의 전문성에 대한 예상대로 decltype()
가 작동하지 않는 이유를 이해할 수 없다 :SFINAE 정적 메서드를 검색하려면
#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
을해야합니다.
전문화가 기본 템플릿 매개 변수에 맞춰져 있다는 사실을 잊어 버렸습니다. 고맙습니다! – eXXXXXXXXXXX2
그냥 쉼표 연산자를 좋아한다 – quetzalcoatl
@quetzalcoatl - 그렇다. – max66