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