在看 《c++ 模板元編程》 的過程中,我時(shí)時(shí)刻刻都會(huì)有新發(fā)現(xiàn)。對(duì)于這之前從未接觸過 Boost 庫(kù)的我來說,驚奇的感覺尤甚。初看此書的習(xí)題4-5:哈,不就是實(shí)現(xiàn)一個(gè) param_type 嘛。的確是,不過 answer 頁上的測(cè)試代碼,才是讓我驚奇的東西:
using namespace boost::assign;
std::vector<int> v1;
v1 += 1, 2, 30, 40, 500;
話不多講,按圖索驥的來分析。既然是 vector 的操作,那就找出對(duì)應(yīng)的頭文件來看看。第一句使用了 boost::assign 命名空間,而 STL 庫(kù)中當(dāng)然不會(huì)提供這樣的 operator+= ,果然在 boost\assign\std\vector.hpp 中有聲明:
template< class V, class A, class V2 >
inline list_inserter< assign_detail::call_push_back< std::vector<V,A> >, V >
operator+=( std::vector<V,A>& c, V2 v ) {
return push_back( c )( v );
}
For the god’s sake ,它一口氣拋給我這么多待查的東西,這不是打擊我的求知欲么!(歇息片刻……)繼續(xù)。先不管這個(gè)重載操作符的返回值,待會(huì)在函數(shù)的返回語句中自然會(huì)了解到。看到 push_back( c )( v ) 這個(gè)句子,當(dāng)然是一個(gè) operator(),繼續(xù)搜索。這一次,VS 代碼查看工具直接把我?guī)У搅四康牡亍?boost\assign\list_inserter.hpp 這塊新大陸。接下來的一切都將在這里發(fā)生。首先我到了 “push_back 站”:
template< class C >
inline list_inserter< assign_detail::call_push_back<C>,
BOOST_DEDUCED_TYPENAME C::value_type >
push_back( C& c ) {
static BOOST_DEDUCED_TYPENAME C::value_type* p = 0;//static typename std::vector<int>::value_type* p = 0;
return make_list_inserter( assign_detail::call_push_back<C>( c ), p );
}
為了方便查看,把 make_list_inserter 和 call_push_back 也列出來:
template< class Function, class Argument >
inline list_inserter<Function,Argument>
make_list_inserter( Function fun, Argument* ) {
return list_inserter<Function, Argument>( fun );
}
template< class C >
class call_push_back {
C& c_; //這里,c_ 是 vector<int>& v1
public:
call_push_back( C& c ) : c_( c ) { }
template< class T >
void operator()( T r ) {
c_.push_back( r );
}
};
過了這條流水線,現(xiàn)在 operator+= 中的 push_back( c ) 就被加工成了:
list_inserter<call_push_back, p>( call_push_back<vector<int> > ) ,其私有成員 insert_ 為 call_push_back<vector<int> > 。前面提到的 operator() 就要登場(chǎng)了,先看 list_inserter 的部分代碼:
template< class Function, class Argument = assign_detail::forward_n_arguments >
class list_inserter {
...
public:
list_inserter( Function fun ) : insert_( fun ) {}
...
list_inserter& operator()() { //這就是那個(gè) operator() ! 終于找到你了
insert_( Argument() );
return *this;
}
...
template< class T >
list_inserter& operator,( const T& r ) { //它化腐朽為神奇,逗號(hào)(,)從以前的“路標(biāo)”升級(jí)成了“交警”
insert_( r );
return *this;
}
... ...
private:
list_inserter& operator=( const list_inserter& );
Function insert_; // call_push_back
};
到這里,第一個(gè) v1 += 1 就是下面這個(gè)樣子:
list_inserter<call_push_back, p>( call_push_back<vector<int> > )( 1 )
相當(dāng)于 call_push_back(1),call_push_back的 c_ 成員就是 v1 的引用,就相當(dāng)于 v1.push_back(1) 。第一個(gè)元素進(jìn)入了容器,鼓掌。
看過代碼后,逗號(hào)( , )就不足為奇了, v1 += 1, 2, 30, 40, 500; 句子中的 “,”, 功能是依次插入逗號(hào)后的元素。
繼續(xù)探索新大陸……
posted on 2009-12-09 11:01
崇文 閱讀(1013)
評(píng)論(0) 編輯 收藏 引用