• <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) 。第一個元素進入了容器,鼓掌。
                看過代碼后,逗號( , )就不足為奇了, v1 += 1, 2, 30, 40, 500; 句子中的 “,”, 功能是依次插入逗號后的元素。
                繼續(xù)探索新大陸……
            posted on 2009-12-09 11:01 崇文 閱讀(1015) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久免费视频1| 草草久久久无码国产专区| 国产免费久久精品99re丫y| 无码人妻久久一区二区三区蜜桃| 久久精品亚洲AV久久久无码| 久久免费国产精品一区二区| 狠狠人妻久久久久久综合蜜桃| 精品多毛少妇人妻AV免费久久 | 欧美噜噜久久久XXX| 一本一道久久精品综合| 久久精品亚洲AV久久久无码| 伊人久久综在合线亚洲2019| 久久精品国产色蜜蜜麻豆| 情人伊人久久综合亚洲| 亚洲乱码中文字幕久久孕妇黑人| 国产成人精品久久一区二区三区| 污污内射久久一区二区欧美日韩| 潮喷大喷水系列无码久久精品| 合区精品久久久中文字幕一区| 久久久精品一区二区三区| 国产成人精品综合久久久| 久久久黄片| 国内精品久久久久久久久电影网| 精品永久久福利一区二区 | 久久精品国产亚洲AV大全| 久久精品一区二区三区中文字幕| 久久国产亚洲精品麻豆| 久久久久亚洲AV无码麻豆| 国内精品综合久久久40p| 久久久中文字幕日本| 精品久久久久久无码国产| 99久久人妻无码精品系列| 久久精品aⅴ无码中文字字幕重口| 久久久亚洲AV波多野结衣| 麻豆av久久av盛宴av| 久久男人中文字幕资源站| 久久人人爽人爽人人爽av | 久久久中文字幕日本| 久久久无码精品亚洲日韩软件| 久久亚洲电影| 久久亚洲国产精品成人AV秋霞 |