• <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  評論-37  文章-0  trackbacks-0

            轉:http://blog.csdn.net/ralph623/archive/2005/10/15/504369.aspx

            在 STL 中有各種容器,而 STL 算法允許我們對容器中的元素做各種操作,下面的程序對于每一個當代的 C++ 程序員都應該是輕而易舉的:

            #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());
            }

            運行結果:

            world
            hello
            hello
            world

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

            MPL 可以看成是 STL 的編譯期版本,或者說元編程版本。它同樣也提供了各種容器,只不過容納的對象不是數據,而是類型。它們的構造方式語法上比較類似,或者甚至,我以為,更有趣一點:

            #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 是什么就不用我說了。mpl::at_c 是一個元編程算法,作用相當于運行期的 [ ] 運算符,也就是得到一個容器中在某個位置上的元素。在 VC7.1 下面,執行結果是

            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> >

            這跟運行期的 list 的行為幾乎完全一致。

            當然,mpl 也有 for_each ,而且我們也可以為 for_each 提供一個元編程 functor 。什么是元編程 functor ?運行時的 functor 是一個提供了 operator() 重載的 struct ,而元編程 functor 就是一個提供了 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 的程序完全相同。

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

            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 把這些類型放在同一個 list 里面當然不在話下,但是我們希望有一個類似 factory 模式的實現,讓我們可以自由創建它們。下面的程序用 mpl::for_each 為 list 中的每一個類型創建一個實例,它當然可以被擴展來做些很有用的事情。

            #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 都沒有默認的 constructor ,必須加上這個
            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


            本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/ralph623/archive/2005/10/15/504369.aspx

            posted on 2010-10-04 18:40 小王 閱讀(4182) 評論(0)  編輯 收藏 引用 所屬分類: Boost
            少妇久久久久久久久久| 久久天天躁狠狠躁夜夜网站| 久久亚洲精品无码VA大香大香| 久久国产亚洲精品无码| 精品国产99久久久久久麻豆| 久久久久久综合网天天| 欧美亚洲色综久久精品国产| 久久久久亚洲av无码专区导航| 亚洲国产欧美国产综合久久| 三上悠亚久久精品| 久久久久亚洲精品无码蜜桃| 久久青草国产精品一区| 国产精品免费久久| 久久99国产一区二区三区| 久久国产视频网| 色诱久久av| 午夜人妻久久久久久久久| 久久国产精品无码一区二区三区| 久久精品夜夜夜夜夜久久| 国产午夜精品理论片久久影视| 久久精品国产91久久综合麻豆自制| 国产亚洲欧美成人久久片| 久久中文精品无码中文字幕| 久久无码高潮喷水| 精品久久久噜噜噜久久久 | 亚洲欧美精品一区久久中文字幕| 中文字幕无码久久人妻| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 国产精品久久久天天影视香蕉| 久久久久久久久久久免费精品| 亚洲va久久久噜噜噜久久天堂 | 浪潮AV色综合久久天堂| 99久久精品国产一区二区三区 | 狠狠色丁香久久婷婷综| 日本精品一区二区久久久| 久久亚洲精精品中文字幕| 久久久久久国产精品美女| 麻豆一区二区99久久久久| 欧美午夜精品久久久久久浪潮| 99久久国产热无码精品免费| 久久久久亚洲av综合波多野结衣 |