c++模板元編程七:類型判斷的traits類

來源:互聯網
上載者:User

標籤:c++   metaprogramming   c++11   boost   

2 用於判斷類型的traits類

is_void類可以判斷模板參數的類型,使用很簡單,注意,也可以直接使用::value。

cout << std::is_void<void>::type::value << endl;cout << std::is_void<bool>::type::value << endl;

但是實現很有趣,特化居然可以用來繼承不同的類。這裡沒有去找clang++實現代碼,用boost代碼代替吧。

template <typename T>struct is_void : public false_type{};template <>struct is_void<void> : public true_type{};

c++11標準支援的同理還有is_pointer, is_floating_point, is_array, is_lvalue_reference, is_rvalue_reference, is_member_object_pointer, is_member_function_pointer, is_enum, is_union, is_class, is_function。實現代碼都很簡單,這裡再展示一個:

template <typename T>struct is_pointer : public false_type{};template <typename T>struct is_pointer<T*> : public true_type{};

c++模板元編程七:類型判斷的traits類

聯繫我們

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