STL雜記(2)

來源:互聯網
上載者:User

 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.