function object研究之七 is_placeholder

來源:互聯網
上載者:User

這裡看一下is_placehoder模板

在is_placeholder.hpp中,有如下定義:

namespace boost{template< class T > struct is_placeholder{    enum _vt { value = 0 };};} 

在arg.hpp中,還有幾個偏特化版本的定義:

#if !defined( BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION )template< int I > struct is_placeholder< arg<I> >{    enum _vt { value = I };};template< int I > struct is_placeholder< arg<I> (*) () >{    enum _vt { value = I };};#endif

is_placeholder的第一個特化版本接受arg<I>作為模板參數,並且將I的值賦值給內部成員value,

第二個特化版本接受函數指標作為模板參數,返回arg<I>, 沒有參數。

如果模板參數T不是arg<I>類型或者函數指標,則使用is_placeholder.hpp中的普通模板定義,將value設定為0.

看下面的代碼示範了如何使用

  int z = boost::is_placeholder<boost::arg<9> >::value;  int q = boost::is_placeholder<string >::value;

z變數的值就是9.而q變數的值就是0,因為string不是arg<I>類型

boost::arg<1> F() {    return _1;}typedef boost::arg<1> (*FPointer)();....cout << boost::is_placeholder<FPointer>::value << endl;

上面的代碼示範了函數指標作為模板參數使用的方法。

總結,is_placeholder模板的作用是:

1.接受模板參數,判斷是否是boost::arg<N>類型或者返回該類型的函數指標

2.如果不是,則value為0

3.如果是,則value為N

聯繫我們

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