http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
#include <iostream>
template <typename T>
struct has_typedef_foobar {
// Types "yes" and "no" are guaranteed to have different sizes,
// specifically sizeof(yes) == 1 and sizeof(no) == 2.
typedef char yes[1];
typedef char no[2];
template <typename C>
static yes& test(typename C::foobar*);
template <typename>
static no& test(...);
// If the "sizeof" the result of calling test<T>(0) would be equal to the sizeof(yes),
// the first overload worked and T has a nested type named foobar.
static const bool value = sizeof(test<T>(0)) == sizeof(yes);
};
struct foo {
typedef float foobar;
};
int main() {
std::cout << std::boolalpha;
std::cout << has_typedef_foobar<int>::value << std::endl;
std::cout << has_typedef_foobar<foo>::value << std::endl;
}
上面的例子显示了 SFAINE 。
- 在这里我无法理解为什么 sizeof(yes)==1 和 sizeof(no)==2。
- 由于测试是静态函数,所以也应该有一些测试函数的定义。但是这里的代码编译得很好,没有定义测试函数