首先看一下bind.hpp中的list0模板定义:
[cpp]
class list0
{
public:
list0() {}
template
template
template
template
template
template
{
return unwrapper
}
template
{
return unwrapper
}
template
{
unwrapper
}
template
{
unwrapper
}
template
{
}
bool operator==(list0 const &) const
{
return true;
}
};
提供了accept方法接受V&,但是什么实现也没有。提供了一堆operator重载函数,重点关注operator()()的函数,说明其实list0就是一个function object。
1.type
[cpp] www.2cto.com
template
只是一个普通模板,接受任何类型。
2.unwrap(f,0)的结果是返回f, 因此如果f是以一个函数传递给了operator()(type
因此实际上代码被编译成了直接调用f的代码:f()
这是个很好的让编译器在编译期产生调用函数代码的技巧。
因此功能比较强大。
3.实际上f函数执行的时候传递进来的参数这里是空,下面就能看到有一个参数的例子。
继续看list1的定义:
template< class A1 > class list1: private storage1< A1 >
{
private:
typedef storage1< A1 > base_type;
public:
explicit list1( A1 a1 ): base_type( a1 ) {}
A1 operator[] (boost::arg<1>) const { return base_type::a1_; }
A1 operator[] (boost::arg<1> (*) ()) const { return base_type::a1_; }
template
template
template
template
template
template
{
return unwrapper
}
template
{
return unwrapper
}
template
{
unwrapper
}
template
{
unwrapper
}
template
{
base_type::accept(v);
}
bool operator==(list1 const & rhs) const
{
return ref_compare(base_type::a1_, rhs.a1_, 0);
}
};
看到很多老朋友了吧?
1.从storage1
因此也就拥有了a1_, 这里有点不明白,为什么偏特化版本的storag1没有定义a1_,只有a1()静态函数,居然这里就有了。下面的代码编译通过。
[cpp]
boost::_bi::storage1
x.a1_;
2. 几个operator[]() 函数有点意思,用_1作为下标,调用的是第一个重载:
[cpp]
list[_1];
-->
A1 operator[] (boost::arg<1>) const { return base_type::a1_; }
3.operator()()函数的参数A & a实际上也是一个list1
a[base_type::a1_] 这句代码要注意:
通过this对象的base_type::a1_,这时候a1_是指针函数,返回占位符boost::arg<1> 。
用a1_来作为参数,传递给a对象的operator[](boost::arg<1> (*)() 方法来获取内部保