最近對(duì)boost的bind部分比較感興趣,對(duì)其背后的機(jī)制進(jìn)行了簡(jiǎn)單的分析,和大家分享一下。
注,我所看的代碼是boost_1_64_0, 想來各個(gè)版本的差異不大。
定義函數(shù)
[cpp]?view plaincopy
int?f(int?a,?int?b)?? {?? ????return?a?+?b;?? }?? ?? int?g(int?a,?int?b,?int?c)?? {?? ????return?a?+?b?+?c;?? }??
調(diào)用范例:
bind(f, 1, 2)(); //f(1,2)
bind(f, _2, _1)(x, y); // f(y, x)bind(g, _1, 9, _1)(x); // g(x, 9, x)bind(g, _3, _3, _3)(x, y, z); // g(z, z, z)bind(g, _1, _1, _1)(x, y, z); // g(x, x, x)
_1, _2, ... _9在 boost中被稱為placeholder,是占位符的意思。它表示參數(shù)。這種方式,我是只在boost中見過,是個(gè)非常神奇的用法。
它們究竟是什么呢?,且看定義:(boost/bind/placeholders.hpp)
[cpp]?view plaincopy
boost::arg<1>?_1;?? boost::arg<2>?_2;?? boost::arg<3>?_3;?? boost::arg<4>?_4;?? boost::arg<5>?_5;?? boost::arg<6>?_6;?? boost::arg<7>?_7;?? boost::arg<8>?_8;?? boost::arg<9>?_9;??
boost::arg也是個(gè)模板,至于是什么樣的模板,留個(gè)懸念吧。
boost bind的這些功能,顛覆了我對(duì)C++的看法,從未想到過,C++還可以這么玩。那么,boost究竟是怎么實(shí)現(xiàn)的呢?
讀者請(qǐng)注意,bind在這里涉及了兩個(gè)參數(shù)表。第一個(gè)參數(shù)表是被bind綁定的函數(shù)(例子中f,g函數(shù))的參數(shù)表,另外一個(gè)是bind生成的新的函數(shù)對(duì)象的參數(shù)表。
這兩個(gè)參數(shù)表如何實(shí)現(xiàn)?如何轉(zhuǎn)換是我們后面分析的重點(diǎn)。
bind是什么?
bind是函數(shù),是非常神奇的函數(shù),不是一個(gè)函數(shù),而是一組函數(shù),是一組重載的函數(shù)。
翻開代碼 boost/bind/bind.hpp,找到BOOST_BIND字符串,大約在1290行的位置,boost定義了bind(1.51.0):
[cpp]?view plaincopy
?? ?? #ifndef?BOOST_BIND?? #define?BOOST_BIND?bind?? #endif?? ?? ?? ?? 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());?? }?? ?? template<class?R,?class?F,?class?A1>?? ????_bi::bind_t<R,?F,?typename?_bi::list_av_1<A1>::type>?? ????BOOST_BIND(F?f,?A1?a1)?? {?? ????typedef?typename?_bi::list_av_1<A1>::type?list_type;?? ????return?_bi::bind_t<R,?F,?list_type>?(f,?list_type(a1));?? }?? ?? ?? template<class?R,?class?F,?class?A1,?class?A2>?? ????_bi::bind_t<R,?F,?typename?_bi::list_av_2<A1,?A2>::type>?? ????BOOST_BIND(F?f,?A1?a1,?A2?a2)?? {?? ????typedef?typename?_bi::list_av_2<A1,?A2>::type?list_type;?? ????return?_bi::bind_t<R,?F,?list_type>?(f,?list_type(a1,?a2));?? }?? ....??
太多了,只貼3個(gè),足以說明問題。
模板參數(shù)
- R 表示返回類型
- F ?表示函數(shù)指針的類型
- A1,A2, .... 這些都是參數(shù)的類型
boost將BOOST_BIND定義為bind。所以,你看到的BOOST_BIND就是對(duì)bind函數(shù)的定義。
bind函數(shù)非常簡(jiǎn)單,它其實(shí)就是返回一個(gè)bind_t類的對(duì)象。bind_t類也是一個(gè)模板類。 如果仔細(xì)觀察,你可以發(fā)現(xiàn),這些不同參數(shù)的bind函數(shù),其實(shí)現(xiàn)上不同在于
list_av_N這一系列的輔助類是不同的。list_av_1表示一個(gè)參數(shù),list_av_2表示兩個(gè)參數(shù), 以此類推。
這里的list_av_N對(duì)象,是第一個(gè)參數(shù)表,是函數(shù)F要求的參數(shù)表,這個(gè)參數(shù)表是可以包含placeholder的。
list_av_N對(duì)象其實(shí)只是一個(gè)包裝對(duì)象。我們以list_av_2為例:
[cpp]?view plaincopy
template<class?A1,?class?A2>?struct?list_av_2?? {????? ????typedef?typename?add_value<A1>::type?B1;?? ????typedef?typename?add_value<A2>::type?B2;?? ????typedef?list2<B1,?B2>?type;?? };???? list_av_2的作用,僅僅是為了包裝而已。list_av_2::type == ist2<A1,A2> == list_type。
bind_t是什么東東?
奧秘在bind_t中,且看bind_t的定義 (也在boost/bind/bind.hpp中。下面只是bind_t的一種定義方法,但是道理都是一樣的)
[html]?view plaincopy
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?? ?????? };?? 模板參數(shù)R代表return type, F代表function type, L表示的是listN(list0,list1,list2,....),這個(gè)是關(guān)鍵啊。
至于bind_template.hpp,這個(gè)源代碼也比較簡(jiǎn)單,主要是定義了operator () 的實(shí)現(xiàn)。
bind_t重載括號(hào)運(yùn)算符,因此,bind_t可以像函數(shù)那樣調(diào)用。而且,
bind_t的operator()有N多個(gè)重載,分別對(duì)應(yīng)的是不同的參數(shù)類型和參數(shù)個(gè)數(shù)。這使得我們可以用不同的參數(shù)調(diào)用bind_t的對(duì)象。
我們摘抄一個(gè)有一兩個(gè)參數(shù)的括號(hào)重載,看看
[cpp]?view plaincopy
.....?? ????template<class?A1>?result_type?operator()(A1?&?a1)?? ????{?? ????????list1<A1?&>?a(a1);?? ????????BOOST_BIND_RETURN?l_(type<result_type>(),?f_,?a,?0);?? ????}?? ....?? ?? ????template<class?A1,?class?A2>?result_type?operator()(A1?&?a1,?A2?&?a2)?? ????{?? ????????list2<A1?&,?A2?&>?a(a1,?a2);?? ????????BOOST_BIND_RETURN?l_(type<result_type>(),?f_,?a,?0);?? ????}?? ......?? f_就是函數(shù)指針,這個(gè)不用多說;l_是 L (listN)對(duì)象。?
請(qǐng)注意,這里有兩個(gè)listN出現(xiàn):
一個(gè)是l_?這是針對(duì)f_提供的參數(shù)列表,其中包含_1,_2,....這樣的placeholder一個(gè)是生成的臨時(shí)變量?a, 這個(gè)是bind_t函數(shù)對(duì)象在調(diào)用時(shí) 的參數(shù)列表
之所以一直強(qiáng)調(diào)兩個(gè)listN,是因?yàn)?#xff0c;奧秘就在listN這些個(gè)類中的。大家請(qǐng)記住這一點(diǎn):
一直存在兩個(gè)listN對(duì)象。
listN的奧秘
bind_t對(duì)象的operator () 調(diào)用的是listN 的operator (),那么,整個(gè)實(shí)現(xiàn),就在listN中,為了方便說明,我們以list2為例。
對(duì)list2的分析,我們只看3部分:
1. 類聲明部分:
[cpp]?view plaincopy
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?)?{}?? 從這個(gè)定義,我們知道,它從storage2繼承,storage2是什么?
[cpp]?view plaincopy
template<class?A1,?class?A2>?struct?storage2:?public?storage1<A1>?? {?? ????typedef?storage1<A1>?inherited;?? ?? ????storage2(?A1?a1,?A2?a2?):?storage1<A1>(?a1?),?a2_(?a2?)?{}?? ?? ????template<class?V>?void?accept(V?&?v)?const?? ????{????? ????????inherited::accept(v);?? ????????BOOST_BIND_VISIT_EACH(v,?a2_,?0);??? ????}????? ?? ????A2?a2_;?? };?? 從名字和定義上,我們就可以斷定:storage2就是保存兩個(gè)參數(shù)的參數(shù)列表對(duì)象。看來,storageN負(fù)責(zé)存儲(chǔ),而listN負(fù)責(zé)如何使用這些參數(shù)了。
2. operator[] 系列重載函數(shù)
[cpp]?view plaincopy
????A1?operator[]?(boost::arg<1>)?const?{?return?base_type::a1_;?}?? ????A2?operator[]?(boost::arg<2>)?const?{?return?base_type::a2_;?}?? ?? ???.....?? ?? ????template<class?T>?T?&?operator[]?(_bi::value<T>?&?v)?const?{?return?v.get();?}?? .....?? 我已經(jīng)剔除了一些定義,只留下我們關(guān)系的定義。
這里面有兩類定義,
針對(duì) boost::arg<1>和boost::arg<2>定義的。其實(shí)就是針對(duì)_1, _2的定義,這個(gè)定義表明:如果是_1,那么,list2就返回存儲(chǔ)的參數(shù)a1, 如果是_2,那么就返回存儲(chǔ)的參數(shù)a2。這些參數(shù),是上面我說的針對(duì)f_函數(shù)的參數(shù);針對(duì)_bi::value<T>的定義。_bi::value<T>就是對(duì)T進(jìn)行簡(jiǎn)單封裝的類型。這個(gè)定義僅僅是將value的值再取出來。
這兩類定義,就是關(guān)鍵所在了。
3. operator()系列重載函數(shù)
[html]?view plaincopy
....?? ????template<class?F,?class?A>?void?operator()(type<void>,?F?&?f,?A?&?a,?int)?? ????{?? ????????unwrapper<F>::unwrap(f,?0)(a[base_type::a1_],?a[base_type::a2_]);?? ????}?? ....?? 盡管有多種不同的重載,但是基本形式就是這樣的。
最后一個(gè)參數(shù)"int"我想沒有直接的意義,可能是為了重載時(shí)用于區(qū)分不同的重載函數(shù)使用的。
unwrap的作用這里可以忽略。你可以認(rèn)為就是直接調(diào)用f。
下面有兩個(gè)不同尋常的語句:
[html]?view plaincopy
a[base_type::a1_],?a[base_type::a2_]?? a是一個(gè)listN對(duì)象,這兩句究竟是什么意思呢?
下面,我們用兩個(gè)例子分別說明,
例子1
bind(f, 1, 2)(); //f(1,2) 下面,我們將參數(shù)代入:這種情況下,我們得到的bind_t對(duì)象,實(shí)際上是
[cpp]?view plaincopy
bind_t<int,?int(*)(int,int),?list2<int,int>?>??? ?? list2<int,int>?=?[?base_type::a1_?=?1,?base_type::a2_?=?2]?? 而bind(f, 1, 2)
?(); 中最后一個(gè)括號(hào),調(diào)用了bind_t的
operator () (void)?:?
[cpp]?view plaincopy
result_type?operator()()?? {?? ????list0?a;?? ????BOOST_BIND_RETURN?l_(type<result_type>(),?f_,?a,?0);?? }?? 因此,a = list0。
在這種情況下,
[cpp]?view plaincopy
a[base_type::a1_]??=?=?? a.operator[]((_bi::value<int>)?1)?? ==?1;?? ?a[base_type::a2_]?==?? a.operator[](_bi::value<int>)?2)?? ==?2;?? 其實(shí)listN里面的operator[](_bi::value<T>)就是打醬油的,目的就是為了在語法上統(tǒng)一。
因此,bind(f, 1, 2) () === f(1,2)的調(diào)用。
例子2
bind(f, _2, _1)(x, y); // f(y, x) 我們?cè)侔褏?shù)代入,這是,得到的bind_t對(duì)象就是
[html]?view plaincopy
bind_t<int,?int(*)(int,?int),?list2<boost::arg<2>,?boost::arg<1>?>?>?? //l_的類型和值是?? list2<boost::arg<2>,boost::arg<1>?>?=?[?base_type::a1_?=?_2,?base_type::a2_?=?_1]?? 哈哈,看到了吧,list2的類型不一定要和F的參數(shù)類型一樣的哦。
當(dāng)調(diào)用bind(f, _2, _1)
(x, y); 中bind_t::operator()(int, int) 的時(shí)候,即
[cpp]?view plaincopy
template<class?A1,?class?A2>?result_type?operator()(A1?&?a1,?A2?&?a2)?? {?? ????list2<A1?&,?A2?&>?a(a1,?a2);?? ????BOOST_BIND_RETURN?l_(type<result_type>(),?f_,?a,?0);?? }?? 此時(shí),a的類型和值是
[cpp]?view plaincopy
list2<int,?int>?=?[?base_type::a1_=?x,?base_type::a2_?=?y]?? 這種情況下,
[html]?view plaincopy
a[l_.base_type::a1_]?==?? ??a?[?_2?]?==?? ?a.operator[]?(?(boost::arg<2>&)_2)?==?? ?a.base_type::a2_?==?? y;?? ?? a[l_.base_type::a2_]?==?? ??a?[?_1?]?==?? ?a.operator[]?(?(boost::arg<1>&)_1)?==?? ?a.base_type::a1_?==?? x;?? 即 bind(f, _2, _1)(x, y) === f(y, x);
關(guān)于_1,_2,_3,...._9
現(xiàn)在,我們要看看,到底 boost::arg<1>, boost::arg<2> ... boost::arg<9>是什么了。
[cpp]?view plaincopy
template<?int?I?>?struct?arg??? {?? ????arg()?? ????{????? ????}????? ?? ????template<?class?T?>?arg(?T?const?&??)?? ????{????? ?????????? ????????typedef?char?T_must_be_placeholder[?I?==?is_placeholder<T>::value??1:?-1?];?? ????}????? };?? 呵呵,什么都沒有!的確,什么都沒有,因?yàn)樗莗lacheholder嘛! 它只要表明自己的類型就可以了。這是為什么在 listN的operator[] 中,boost::arg<N> 沒有定義形慘了。
第二個(gè)帶有 T const & 參數(shù)的構(gòu)造函數(shù)是什么?看起來很奇怪,其實(shí),它是拷貝構(gòu)造函數(shù)。
看看is_placeholder的定義吧:
[cpp]?view plaincopy
template<?int?I?>?struct?is_placeholder<?arg<I>?>?? {?? ????enum?_vt?{?value?=?I?};?? };?? 它的作用,是防止錯(cuò)誤的參考構(gòu)造。
假如,你這樣定義:
[cpp]?view plaincopy
boost::arg<2>?arg2(_1);?? 模板展開后,將是這樣的
[cpp]?view plaincopy
struct?arg?<2>?? {??? .....?? ????arg(?arg<1>?const?&??)?? ????{????? ?????????? ????????typedef?char?T_must_be_placeholder[?I?==?is_placeholder<arg<1>?>::value??1:?-1?];?? ????}????? };?? is_placeholder<arg<1> >::value == 1,而I == 2,因此,你會(huì)得到一個(gè)
[cpp]?view plaincopy
typedef?char?T_must_be_placeholder[?-1?];?? 因此,你將收到一個(gè)編譯錯(cuò)誤。僅此而已。
其他
到此為止,boost bind的關(guān)鍵部分就已經(jīng)清楚了。boost還有些高級(jí)議題,如類的成員函數(shù)的綁定、變量引用、綁定嵌套等等。這些議題都是以此為基礎(chǔ),再增加了一些新模板參數(shù)而已,已經(jīng)不是實(shí)現(xiàn)的核心了,有興趣的同學(xué)可以自己看看。
總結(jié)
以上是生活随笔為你收集整理的boost中bind的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。