SFINAE
This "substitution-failure-is-not-an-error" (SFINAE) principle is clearly an important ingredient to make the overloading of function templates practical.
typedef char RT1;
typedef struct { char a[2]; } RT2;
template<typename T> RT1 test(typename T::X const*);
template<typename T> RT2 test(...);
class _Cls{
public
typedef char X; //traits
};
test<_Cls>(0);
#define type_has_member_type_X(T) /
(sizeof(test<T>(0)) == 1)
(type_has_member_type_X(_Cls))
? std::cout << "YES/n"
: std::cout << "NO/n";
这里会输出YES,说明_Cls是符合“一个有内部类型是X的类型”要求的类型。
(用处:能够判断一个类型是否是符合某一“特性”的类型,发挥这一作用的关键是找到这个“特性”)
To allow overload resolution of function templates using type traits rather than the type itself.
This is an acronym that means "substitution-failure-is-not-an-error" and it is used in the context of template functions overloading. When the compiler evaluates each overloaded template functions, it will not emit an error if by using the template parameter on one of the potential candidate function would generate an error. The compiler will just discard that function from the list of potential candidates.
老外说的
http://www.martinecker.com/wiki/index.php?title=SFINAE_Principle