2
我試圖實現一種機制來檢測提供的類是否包含一些靜態方法。這是很簡單的代碼,但我不明白爲什麼decltype()
如預期EnableIfHasFooMethod
類的專業化不起作用: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