function object研究之list1分析(一)

2014-11-24 08:53:13 · 作者: · 浏览: 2

首先看一下bind.hpp中的list0模板定义:

[cpp]
class list0
{
public:

list0() {}

template T & operator[] (_bi::value & v) const { return v.get(); }

template T const & operator[] (_bi::value const & v) const { return v.get(); }

template T & operator[] (reference_wrapper const & v) const { return v.get(); }

template typename result_traits::type operator[] (bind_t & b) const { return b.eva l(*this); }

template typename result_traits::type operator[] (bind_t const & b) const { return b.eva l(*this); }

template R operator()(type, F & f, A &, long)
{
return unwrapper::unwrap(f, 0)();
}

template R operator()(type, F const & f, A &, long) const
{
return unwrapper::unwrap(f, 0)();
}

template void operator()(type, F & f, A &, int)
{
unwrapper::unwrap(f, 0)();
}

template void operator()(type, F const & f, A &, int) const
{
unwrapper::unwrap(f, 0)();
}

template void accept(V &) const
{
}

bool operator==(list0 const &) const
{
return true;
}
};

提供了accept方法接受V&,但是什么实现也没有。提供了一堆operator重载函数,重点关注operator()()的函数,说明其实list0就是一个function object。

1.type是什么?定义如下:

[cpp] www.2cto.com
template class type {};
只是一个普通模板,接受任何类型。
2.unwrap(f,0)的结果是返回f, 因此如果f是以一个函数传递给了operator()(type, F& f, A&, long) ,则内部的unwrap(f,0)返回的是f

因此实际上代码被编译成了直接调用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 T & operator[] ( _bi::value & v ) const { return v.get(); }

template T const & operator[] ( _bi::value const & v ) const { return v.get(); }

template T & operator[] (reference_wrapper const & v) const { return v.get(); }

template typename result_traits::type operator[] (bind_t & b) const { return b.eva l(*this); }

template typename result_traits::type operator[] (bind_t const & b) const { return b.eva l(*this); }

template R operator()(type, F & f, A & a, long)
{
return unwrapper::unwrap(f, 0)(a[base_type::a1_]);
}

template R operator()(type, F const & f, A & a, long) const
{
return unwrapper::unwrap(f, 0)(a[base_type::a1_]);
}

template void operator()(type, F & f, A & a, int)
{
unwrapper::unwrap(f, 0)(a[base_type::a1_]);
}

template void operator()(type, F const & f, A & a, int) const
{
unwrapper::unwrap(f, 0)(a[base_type::a1_]);
}

template void accept(V & v) const
{
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(_1);
x.a1_;
2. 几个operator[]() 函数有点意思,用_1作为下标,调用的是第一个重载:

[cpp]
list[_1];

-->

A1 operator[] (boost::arg<1>) const { return base_type::a1_; }

3.operator()()函数的参数A & a实际上也是一个list1对象,并且里面的a_成员就是外部实际调用的参数。

a[base_type::a1_] 这句代码要注意:

通过this对象的base_type::a1_,这时候a1_是指针函数,返回占位符boost::arg<1> 。

用a1_来作为参数,传递给a对象的operator[](boost::arg<1> (*)() 方法来获取内部保