function object研究之七 is_placeholder

来源:互联网 发布:linux 查看历史登陆 编辑:程序博客网 时间:2024/05/18 03:34

这里看一下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


原创粉丝点击