bind實(shí)質(zhì)上是一個(gè)函數(shù)。
#ifndef BOOST_BIND
#define BOOST_BIND bind
#endif
在此文件內(nèi)將其定義為BOOST_BIND.以下均以BOOST_BIND代指bind.
template<class R, class F>
_bi::bind_t<R, F, _bi::list0>
BOOST_BIND(F f)
{
typedef _bi::list0 list_type;
return _bi::bind_t<R, F, list_type> (f, list_type());
}
此函數(shù)的返回值是_bi::bind_t<R,F,_bi::list0>,參數(shù)是F,這是針對(duì)無(wú)參函數(shù)的版本。
_bi是文件中定義于boost命名空間之下的一個(gè)命名空間,bind實(shí)現(xiàn)的細(xì)節(jié)均在其中(bind implementation).
下面看bind_t定義:
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 <boost/bind/bind_template.hpp>
#undef BOOST_BIND_RETURN
};
看起來(lái)很小,只有一個(gè)類型定義和一個(gè)構(gòu)造函數(shù)。但注意include那一句,這意味著整個(gè)bind_template.hpp文件都
是bind_t類的一部分。而bind_tmplate文件差不多有350行。因?yàn)楹瘮?shù)體都很小,所以直接在類內(nèi)定義,成為inline.所以,當(dāng)要定義的一個(gè)類特別大時(shí),將其具體的函數(shù)
實(shí)現(xiàn)放置于另一個(gè)文件中也是一個(gè)很好的辦法。
下面來(lái)看bind_template.hpp中屬于_bi::bind_t的一些成員定義。
typedef typename result_traits<R, F>::type result_type; result_traits
提取返回值。在bind.hpp中將其定義為R。
result_type operator()()
{
list0 a;
BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}
后面依次是operator()的const版本,一直到九個(gè)參數(shù)的版本。BOOST_BIND_RETURN可以直接當(dāng)作return.
list0類也有九個(gè)。
看一個(gè)例子
int f()
{
return 1;
}
bind(f)()應(yīng)該等于f().
bind函數(shù)中返回值是: _bi::bind_t<R, F, list_type> (f, list_type());list_type=_bi::list0。所以返回值是一個(gè)特化的模板類。
f的類型為int (*)(),即是F。所以返回值是
_bi::bind_t<int, int(*)(),_bi::list0>(f,_bi::list0());
R為萃取出的返回值(int)。通過(guò)此構(gòu)造函數(shù),構(gòu)造出了一個(gè)bind_t類,f,list0,分別由bind_t的相應(yīng)私有數(shù)據(jù)存儲(chǔ)起來(lái)。
然后是調(diào)用此類(函數(shù)體).即bind(f)(),bind_t重載了相應(yīng)的()運(yùn)算符。
還是依參數(shù)個(gè)數(shù)不同有不同的表現(xiàn)形式,此例沒(méi)有參數(shù),其表現(xiàn)形式為:
result_type operator()()
{
list0 a;
BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}
返回值即為原來(lái)的int.函數(shù)實(shí)現(xiàn)這一塊交給list0(l_).list0(type<int>(),f,a,0).
type<int>()生成了一個(gè)空的類。l_已經(jīng)是一個(gè)生成的類,所以此處調(diào)用的是重載操作符().
template<class R, class F, class A> R operator()(type<R>, F & f, A &, long)
{
return unwrapper<F>::unwrap(f, 0)();
}
又調(diào)用unwrapper結(jié)構(gòu)體。因?yàn)槭莝tatic,所以不用再生成類的實(shí)例,最后返回的就是原來(lái)的那個(gè)f()------->int。
template<class F> struct unwrapper
{
static inline F & unwrap( F & f, long )
{
return f;
}
template<class F2> static inline F2 & unwrap( reference_wrapper<F2> rf, int )
{
return rf.get();
}
template<class R, class T> static inline _mfi::dm<R, T> unwrap( R T::* pm, int )
{
return _mfi::dm<R, T>( pm );
}
};
其他參數(shù)不同的情況與此類似。bind對(duì)不同數(shù)目的參數(shù)都有相應(yīng)一致的處理方式。但是依據(jù)相應(yīng)類的個(gè)數(shù)以及bind的重載個(gè)數(shù),參數(shù)值最多只能有9個(gè)。這樣限制一般也
沒(méi)什么大的影響,因?yàn)橐话愕膮?shù)個(gè)數(shù)沒(méi)有這么多。如果用的參數(shù)有太多的話,其實(shí)可以在源碼上再加上一種重載形式即可。
上面的實(shí)現(xiàn)繞了這么大的一圈,其實(shí)最后調(diào)用的還是原來(lái)的那個(gè)函數(shù),看似費(fèi)時(shí),其實(shí)都是為了泛型的必要。bind能夠綁定的函數(shù)類型大大地增加,不管是普通的函數(shù)
指針,還是函數(shù)體,以及沒(méi)有result_type的類型,bind都可以很好的運(yùn)作。而且還可以與ref庫(kù)結(jié)合起來(lái)使用。
在C++的這些第三方庫(kù)里面,BOOST是比較特別的一個(gè)。因?yàn)樗⒉皇菍W⒂谝粋€(gè)問(wèn)題,而是涉及到了語(yǔ)言的各個(gè)層面。有很多接近于語(yǔ)言底層的特性,所以BOOST庫(kù)比
任何一個(gè)庫(kù)都更值得我們?nèi)パ芯浚Α?p>