在看 《c++ 模板元編程》 的過程中,我時時刻刻都會有新發現。對于這之前從未接觸過 Boost 庫的我來說,驚奇的感覺尤甚。初看此書的習題4-5:哈,不就是實現一個 param_type 嘛。的確是,不過 answer 頁上的測試代碼,才是讓我驚奇的東西:
using namespace boost::assign;
std::vector<int> v1;
v1 += 1, 2, 30, 40, 500;
話不多講,按圖索驥的來分析。既然是 vector 的操作,那就找出對應的頭文件來看看。第一句使用了 boost::assign 命名空間,而 STL 庫中當然不會提供這樣的 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 ,它一口氣拋給我這么多待查的東西,這不是打擊我的求知欲么!(歇息片刻……)繼續。先不管這個重載操作符的返回值,待會在函數的返回語句中自然會了解到。看到 push_back( c )( v ) 這個句子,當然是一個 operator(),繼續搜索。這一次,VS 代碼查看工具直接把我帶到了目的地—— boost\assign\list_inserter.hpp 這塊新大陸。接下來的一切都將在這里發生。首先我到了 “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 );
}
};
過了這條流水線,現在 operator+= 中的 push_back( c ) 就被加工成了:
list_inserter<call_push_back, p>( call_push_back<vector<int> > ) ,其私有成員 insert_ 為 call_push_back<vector<int> > 。前面提到的 operator() 就要登場了,先看 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()() { //這就是那個 operator() ! 終于找到你了
insert_( Argument() );
return *this;
}
...
template< class T >
list_inserter& operator,( const T& r ) { //它化腐朽為神奇,逗號(,)從以前的“路標”升級成了“交警”
insert_( r );
return *this;
}
... ...
private:
list_inserter& operator=( const list_inserter& );
Function insert_; // call_push_back
};
到這里,第一個 v1 += 1 就是下面這個樣子:
list_inserter<call_push_back, p>( call_push_back<vector<int> > )( 1 )
相當于 call_push_back(1),call_push_back的 c_ 成員就是 v1 的引用,就相當于 v1.push_back(1) 。第一個元素進入了容器,鼓掌。
看過代碼后,逗號( , )就不足為奇了, v1 += 1, 2, 30, 40, 500; 句子中的 “,”, 功能是依次插入逗號后的元素。
繼續探索新大陸……
posted on 2009-12-09 11:01
崇文 閱讀(1004)
評論(0) 編輯 收藏 引用