• <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)  編輯 收藏 引用
            久久精品国产清自在天天线| 一本伊大人香蕉久久网手机| 久久人人爽人人爽人人片AV麻烦| 久久天天躁狠狠躁夜夜不卡| 99久久综合狠狠综合久久止| 久久亚洲国产成人影院| 亚洲精品无码久久久久| 一级做a爰片久久毛片人呢| 亚洲国产精品无码久久青草| 91精品国产综合久久精品| 一本久久精品一区二区| 精品午夜久久福利大片| 国内精品久久久久影院亚洲| 7国产欧美日韩综合天堂中文久久久久| 久久综合九色综合欧美就去吻| 久久精品夜夜夜夜夜久久| 国产精品中文久久久久久久| 91久久精品国产成人久久| 久久男人Av资源网站无码软件| 亚洲欧美国产精品专区久久| 国产成人精品久久一区二区三区av| 成人久久免费网站| 久久中文字幕人妻丝袜| 99久久婷婷国产一区二区| 青青草国产精品久久久久| 久久天天躁狠狠躁夜夜96流白浆 | 88久久精品无码一区二区毛片 | 精品人妻伦九区久久AAA片69| 久久精品视频91| 欧美久久一级内射wwwwww.| 狠狠色综合网站久久久久久久| 久久九九全国免费| 99久久精品无码一区二区毛片| 国产精品欧美久久久天天影视| 精品无码久久久久久尤物| 青草国产精品久久久久久| 国产婷婷成人久久Av免费高清| 精品国际久久久久999波多野| 97精品国产91久久久久久| 亚洲伊人久久大香线蕉苏妲己| 天天爽天天爽天天片a久久网|