boost::bind源码剖析(实例分析)

 

 
bind的本质:就是创建一个Functor对象(重载了operator()的类对象)

通过将多余的函数参数和函数指针存储为Funtor对象的成员变量,在调用operator()函数是,利用存储的函数指针和函数参数,返回相应的结果

创建Functor对象
> BoostPrj.exe!bind_test() Line 60 + 0x16 bytes C++
#include  
auto myf = boost::bind(f, 1, 2);
//或者 boost::function myf = boost::bind(f, 1, 2);
myf();

// boost/bind/bind.hpp的代码
#ifndef BOOST_BIND
#define BOOST_BIND bind
#endif

#define BOOST_BIND_CC
#define BOOST_BIND_ST
#include
#undef BOOST_BIND_CC
#undef BOOST_BIND_ST
------------------第二层
BoostPrj.exe!boost::bind(int* f, void a1, bool a2) Line 39 + 0x59 bytes C++
//bind/bind_cc.hpp
template < class R, class B1, class B2, class A1, class A2>
   _bi::bind_t
 BOOST_BIND_ST R (BOOST_BIND_CC *) (B1, B2),
               typename _bi::list_av_2::type> //返回的Functor类型
   BOOST_BIND(BOOST_BIND_ST R (BOOST_BIND_CC *f) (B1, B2), A1 a1, A2 a2)
{
   typedef BOOST_BIND_ST R (BOOST_BIND_CC *F) (B1, B2);
   typedef typename _bi::list_av_2::type list_type;
   return _bi::bind_t (f, list_type(a1, a2));
}
此时:a1=1,a2=2
boost::_bi::bind_t<
int, //R
int (__cdecl*)(int,int),//F 函数指针f
boost::_bi::list2,boost::_bi::value > //list_type 存储2个函数参数
>//返回的Functor类型
--------------------
> BoostPrj.exe!boost::_bi::bind_t,boost::_bi::value > >::bind_t,boost::_bi::value > >(int* f, void l) Line 870 C++
template<class R, class F, class L> class bind_t
{
public:
   typedef bind_t this_type;
   bind_t(F f, L const & l): f_(f), l_(l) {}
#define BOOST_BIND_RETURN return
#include
#undef BOOST_BIND_RETURN

};
//boost/bind/bind_template.hpp
result_type operator()() const
{
       list0 a;
       BOOST_BIND_RETURN l_(type(), f_, a, 0);
}
private:
   F f_;//存储函数指针
   L l_;//存储函数参数

int //R
int (__cdecl*)(int,int)//F
boost::_bi::list2,boost::_bi::value > //L

----------------list_type(a1,a2)的推导过程  第三层
BoostPrj.exe!
boost::_bi::list2,boost::_bi::value >::
           list2,boost::_bi::value >
           (boost::_bi::value a1, boost::_bi::value a2)
   Line 283 + 0x13 bytes C++
//bind/bind.hpp
template<class A1, class A2> struct list_av_2
{
   typedef typename add_value::type B1; //第1
   typedef typename add_value::type B2;
   typedef list2 type;
};
A1和A2为int,B1和B2的类型经如下推导为_bi::value
----------------------
template< class T, int I > struct add_value_2
{
   typedef boost::arg type;
};
template< class T > struct add_value_2< T, 0 >//第4
{
   typedef _bi::value< T > type;
};
template<class T> struct add_value //第2
{
   typedef typename add_value_2< T, boost::is_placeholder< T >::value >::type type;
};
T为int,boost::is_placeholder< T >::value 为0
//boost/is_placeholder.hpp
template< class T > struct is_placeholder
{
   enum _vt { value = 0 };//第3
};
--------------------------------
template<class T> class value{
public:
   value(T const & t): t_(t) {}
   T & get() { return t_; }
   T const & get() const { return t_; }
   bool operator==(value const & rhs) const{
       return t_ == rhs.t_;
   }
private:
   T t_;
};
-----------
template< class A1, class A2 > class list2: private storage2< A1, A2 >{
private:
   typedef storage2< A1, A2 > base_type;
public:
   list2( A1 a1, A2 a2 ): base_type( a1, a2 ) {}
   //
};
A1和A2都是boost::_bi::value
为list2,boost::_bi::value >

转载于:https://my.oschina.net/cppblog/blog/8917

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值