什麼是SFINAE?
Substitution failure is not an error,匹配失敗並不是錯誤,意思是用函數模板匹配規則來判斷類型的某個屬性是否存在,也就是說SFINAE可以作為一種編譯期的不完整內省方法
具體參見http://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
案例:
使用SFINAE判斷模板參數是否是class(在很多場合這個trick非常有用):
template<typename T>class isClassA{ typedef char _One; typedef struct{char a[2];}_Two; template<typename T> static _One isClass(int T::* p); template<typename T> static _Two isClass(...);public: static const int value=sizeof(isClass<T>(NULL))==sizeof(_One); };
使用SFINAE判斷類是否有某個函數:
template<typename T>class HasFuncFoo{ typedef char _One; typedef struct{char a[2];}_Two; template<typename T,void (T::*)()> struct FuncMatcher; template<typename T> static _One hasFunc(FuncMatcher<T,&T::fooFunc>*); template<typename T> static _Two hasFunc(...);public: static const int value=sizeof(hasFunc<T>(NULL))==sizeof(_One); };
如何根據自己的需要寫SFINAE?
基本思路是想辦法把要判斷的東西放到函數參數中,比如上面的FuncMathcer就是很好的例子:把fooFunc作為一個模板類的模板參數,然後以這個類作為SFINAE函數的參數
SFINAE應用
1 tr1的enable_if用了很多SFIANE,遇到類似的情境可以考慮使用SFIANE
2 其他應用有待研究
ps.
看到一個很有意思的c++wikibook:http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms,各種奇技淫巧