c++模板之SFINAE

來源:互聯網
上載者:User

什麼是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,各種奇技淫巧

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.