• <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>
            隨筆-380  評(píng)論-37  文章-0  trackbacks-0

            轉(zhuǎn):http://blog.csdn.net/ralph623/archive/2005/10/15/504369.aspx

            在 STL 中有各種容器,而 STL 算法允許我們對(duì)容器中的元素做各種操作,下面的程序?qū)τ诿恳粋€(gè)當(dāng)代的 C++ 程序員都應(yīng)該是輕而易舉的:

            #include <iostream>
            #include <list>
            #include <algorithm>
            #include <string>

            using namespace std;

            struct print
            {
                void operator()(const string& _str)
                {
                    cout << _str << endl;
                }
            };

            int main()
            {
                list<string> str_list;
                str_list.push_front("hello");
                str_list.push_front("world");
               
                list<string> another_list;
                another_list.push_back("hello");
                another_list.push_back("world");
               
                for_each(str_list.begin(), str_list.end(), print());
                for_each(another_list.begin(), another_list.end(), print());
            }

            運(yùn)行結(jié)果:

            world
            hello
            hello
            world

            簡(jiǎn)單的東西往往能說(shuō)明深刻的道理,在這個(gè)程序里,我們遇到的本質(zhì)問(wèn)題是什么?首先,我們有一個(gè)容器;其次,我們可以往容器里面放東西,最后,我們可以通過(guò)算法把一個(gè)操作施加于這個(gè)容器中的每一個(gè)(也可以是部分)元素中。這就是上面程序中凝結(jié)的本質(zhì)問(wèn)題。

            MPL 可以看成是 STL 的編譯期版本,或者說(shuō)元編程版本。它同樣也提供了各種容器,只不過(guò)容納的對(duì)象不是數(shù)據(jù),而是類型。它們的構(gòu)造方式語(yǔ)法上比較類似,或者甚至,我以為,更有趣一點(diǎn):

            #include <string>
            #include <iostream>
            #include <boost/mpl/at.hpp>
            #include <boost/mpl/list.hpp>
            #include <boost/mpl/push_front.hpp>

            using namespace boost;

            int main()
            {
                typedef mpl::list<> type_list1;
                typedef mpl::push_front<type_list1, int>::type type_list2;
                typedef mpl::push_front<type_list2, std::string>::type type_list;
               
                // 或者這樣更好
                typedef mpl::list<int, std::string> another_list;
               
                std::cout << typeid(mpl::at_c<type_list, 0>::type).name() << std::endl;
                std::cout << typeid(mpl::at_c<type_list, 1>::type).name() << std::endl;
               
                std::cout << typeid(mpl::at_c<another_list, 0>::type).name() << std::endl;
                std::cout << typeid(mpl::at_c<another_list, 1>::type).name() << std::endl;
            }

            稍微解釋一下。mpl::list 就是 std::list 的元編程版本,而 mpl::push_front 是什么就不用我說(shuō)了。mpl::at_c 是一個(gè)元編程算法,作用相當(dāng)于運(yùn)行期的 [ ] 運(yùn)算符,也就是得到一個(gè)容器中在某個(gè)位置上的元素。在 VC7.1 下面,執(zhí)行結(jié)果是

            class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
            int
            int
            class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >

            這跟運(yùn)行期的 list 的行為幾乎完全一致。

            當(dāng)然,mpl 也有 for_each ,而且我們也可以為 for_each 提供一個(gè)元編程 functor 。什么是元編程 functor ?運(yùn)行時(shí)的 functor 是一個(gè)提供了 operator() 重載的 struct ,而元編程 functor 就是一個(gè)提供了 operator() 模板的 struct :

            #include <string>
            #include <iostream>
            #include <boost/mpl/at.hpp>
            #include <boost/mpl/list.hpp>
            #include <boost/mpl/push_front.hpp>
            #include <boost/mpl/for_each.hpp>

            using namespace boost;

            struct print
            {
                template <class T>
                void operator()(const T&)
                {
                    std::cout << typeid(T).name() << std::endl;
                }
            };

            int main()
            {
                typedef mpl::list<> type_list1;
                typedef mpl::push_front<type_list1, int>::type type_list2;
                typedef mpl::push_front<type_list2, std::string>::type type_list;
               
                typedef mpl::list<int, std::string> another_list;
               
                mpl::for_each<type_list>(print());
                mpl::for_each<another_list>(print());
            }

            輸出與上面使用 mpl::at_c 的程序完全相同。

            當(dāng)然,到現(xiàn)在為止,這些程序都還是只停留在純粹的玩具程序上,能不能做點(diǎn)稍微有用的事情呢?當(dāng)然可以。假定我們有這樣一個(gè)繼承體系:根是一個(gè)抽象類 Product ,它有一些派生類,例如 PC , Printer 等等,它們的公共方法 SerialNo 會(huì)返回自己的產(chǎn)品序列號(hào),而這個(gè)序列號(hào)是在構(gòu)造的時(shí)候決定的:

            class Product
            {
            public:
                virtual std::string SerialNo()const = 0;
            };

            class PC : public Product
            {
            public:
                PC(const std::string& _sn)
                    : sn_(_sn)
                {}

                std::string SerialNo()const
                {
                    return sn_;
                }
            private:
                std::string sn_;
            };

            class Printer : public Product
            {
            public:
                Printer(const std::string& _sn)
                    : sn_(_sn)
                {}
               
                std::string SerialNo()const
                {
                    return sn_;
                }
            private:
                std::string sn_;
            };

            用 mpl::list 把這些類型放在同一個(gè) list 里面當(dāng)然不在話下,但是我們希望有一個(gè)類似 factory 模式的實(shí)現(xiàn),讓我們可以自由創(chuàng)建它們。下面的程序用 mpl::for_each 為 list 中的每一個(gè)類型創(chuàng)建一個(gè)實(shí)例,它當(dāng)然可以被擴(kuò)展來(lái)做些很有用的事情。

            #include <string>
            #include <sstream>
            #include <iostream>
            #include <algorithm>
            #include <list>
            #include <boost/shared_ptr.hpp>
            #include <boost/mpl/at.hpp>
            #include <boost/mpl/list.hpp>
            #include <boost/mpl/for_each.hpp>

            using namespace boost;

            class Product
            {
            public:
                virtual std::string SerialNo()const = 0;
            };

            class PC : public Product
            {
            public:
                PC(const std::string& _sn)
                    : sn_(_sn)
                {}

                std::string SerialNo()const
                {
                    return sn_;
                }
            private:
                std::string sn_;
            };

            class Printer : public Product
            {
            public:
                Printer(const std::string& _sn)
                    : sn_(_sn)
                {}
               
                std::string SerialNo()const
                {
                    return sn_;
                }
            private:
                std::string sn_;
            };

            struct print
            {
                template <class T>
                void operator()(const T& product)
                {
                    std::cout << "Type: " << typeid(T).name()
                              << " SerialNo: " << product.SerialNo() << std::endl;
                }
            };

            // 由于 PC 和 Print 都沒(méi)有默認(rèn)的 constructor ,必須加上這個(gè)
            template <class T>
            struct wrap {};

            struct Create
            {
                Create(const std::string& _line)
                    : line_(_line)
                    , serial_(0)
                {}
               
                template <class T>
                void operator()(wrap<T>)
                {
                    std::stringstream ss;
                    ss << line_ << '_' << serial_++;
                    shared_ptr<T> product(new T(ss.str()));
                   
                    print()(*product);
                }
               
                std::string line_;
                unsigned long serial_;
            };

            int main()
            {  
                typedef mpl::list<Printer, PC> product_list;
               
                mpl::for_each<product_list, wrap<mpl::_1> >(Create("line1"));
            }

            輸出:

            Type: class Printer SerialNo: line1_0
            Type: class PC SerialNo: line1_1


            本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/ralph623/archive/2005/10/15/504369.aspx

            posted on 2010-10-04 18:40 小王 閱讀(4179) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Boost
            日本精品久久久久中文字幕8| 亚洲欧美成人久久综合中文网 | 久久精品免费一区二区三区| 久久人妻AV中文字幕| 色婷婷狠狠久久综合五月| 精品多毛少妇人妻AV免费久久| 国产婷婷成人久久Av免费高清| 久久综合国产乱子伦精品免费| 亚洲va久久久噜噜噜久久| 亚洲AV无码久久| 久久99久久99精品免视看动漫| 麻豆亚洲AV永久无码精品久久| 久久人妻少妇嫩草AV无码专区| 久久国产热精品波多野结衣AV| 久久91精品久久91综合| 国产99久久久国产精免费| 久久久久一本毛久久久| 亚洲精品国产自在久久| 国产69精品久久久久APP下载| 久久综合亚洲色HEZYO社区 | 久久亚洲国产成人影院| 亚洲综合熟女久久久30p| 久久99精品久久久久久hb无码| 一级做a爱片久久毛片| 欧美性大战久久久久久| 久久精品国产亚洲AV蜜臀色欲| 少妇内射兰兰久久| 26uuu久久五月天| 色8激情欧美成人久久综合电| 久久亚洲欧美国产精品| 久久国产影院| 国产精品免费看久久久| 精品久久综合1区2区3区激情| 久久久久av无码免费网| 久久久久久久尹人综合网亚洲| 亚洲国产成人久久综合野外| 久久精品国产亚洲av日韩| 亚洲人成无码www久久久| 久久电影网2021| 国内精品综合久久久40p| 91久久精品国产成人久久|