• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-14  評論-8  文章-0  trackbacks-0

                 在看 《c++ 模板元編程》 的過程中,我時時刻刻都會有新發(fā)現(xiàn)。對于這之前從未接觸過 Boost 庫的我來說,驚奇的感覺尤甚。初看此書的習(xí)題4-5:哈,不就是實現(xiàn)一個 param_type 嘛。的確是,不過 answer 頁上的測試代碼,才是讓我驚奇的東西:

            using namespace boost::assign;
            std::vector<int> v1;
            v1 += 1, 2, 30, 40, 500;
               話不多講,按圖索驥的來分析。既然是 vector 的操作,那就找出對應(yīng)的頭文件來看看。第一句使用了 boost::assign 命名空間,而 STL 庫中當(dāng)然不會提供這樣的 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ù)。先不管這個重載操作符的返回值,待會在函數(shù)的返回語句中自然會了解到??吹?push_back( c )( v ) 這個句子,當(dāng)然是一個 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() 就要登場了,先看 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 ) { //它化腐朽為神奇,逗號(,)從以前的“路標(biāo)”升級成了“交警”
                    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 )
                相當(dāng)于 call_push_back(1),call_push_back的 c_ 成員就是 v1 的引用,就相當(dāng)于 v1.push_back(1) 。第一個元素進(jìn)入了容器,鼓掌。
                看過代碼后,逗號( , )就不足為奇了, v1 += 1, 2, 30, 40, 500; 句子中的 “,”, 功能是依次插入逗號后的元素。
                繼續(xù)探索新大陸……
            posted on 2009-12-09 11:01 崇文 閱讀(1022) 評論(0)  編輯 收藏 引用
            中文字幕无码免费久久| 777久久精品一区二区三区无码 | 久久久久AV综合网成人| 久久精品水蜜桃av综合天堂 | 久久精品午夜一区二区福利| 99久久综合狠狠综合久久| 香蕉久久AⅤ一区二区三区| 久久精品人成免费| 日韩十八禁一区二区久久| 99久久人妻无码精品系列| 色天使久久综合网天天| 国产成人无码精品久久久久免费| 久久精品国产亚洲AV影院| 久久久久香蕉视频| 国产精品视频久久久| 伊人久久大香线蕉av不变影院| 久久精品无码一区二区日韩AV | 久久精品国产秦先生| 色偷偷偷久久伊人大杳蕉| 怡红院日本一道日本久久 | 国内精品久久久久影院亚洲| 亚洲一本综合久久| 精品一区二区久久久久久久网站| 国产偷久久久精品专区| 久久人妻少妇嫩草AV蜜桃| 欧美一级久久久久久久大| 精品久久人人做人人爽综合| 品成人欧美大片久久国产欧美...| 国产精品久久久久久| 久久福利青草精品资源站| 精品无码久久久久久尤物| 久久国产欧美日韩精品| 欧美噜噜久久久XXX| 波多野结衣中文字幕久久| 成人国内精品久久久久一区| 精品久久久久久国产| 嫩草影院久久99| 久久er国产精品免费观看8| 久久伊人五月天论坛| 性做久久久久久久久久久| 久久人妻少妇嫩草AV蜜桃|