• <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>
            posts - 23,  comments - 94,  trackbacks - 0
            /************************************************************************/
            /* Copyright (c) 2009, Roc King
            All rights reserved.

            Redistribution and use in source and binary forms,
                with or without modification, are permitted
                provided that the following conditions are met:

            1. Redistributions of source code must retain the above copyright notice,
                this list of conditions and the following disclaimer.

            2. Redistributions in binary form must reproduce the above copyright notice,
                this list of conditions and the following disclaimer in the documentation
                and other materials provided with the distribution.

            3. Neither the name of the Tju nor the names of its contributors
                may be used to endorse or promote products derived from this software
                without specific prior written permission.

            THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
                AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
                INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
                INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
                (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
                LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                (INCLUDING NEGLIGENCE OR OTHERWISE)
                ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
                EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                                                                     
            */
            /************************************************************************/


            /**
            這份代碼詳細介紹了使用SFINAE技術實現is_not_buildin_type的原理。
            按順序由上往下閱讀即可。
            */

            #include 
            <stdio.h>
            #include 
            <iostream>


            /** ------------------------------------------------------------------------ */
            #define DEFINITION(prefix)          \
            prefix none {};                     \
            prefix data { 
            int i; };             \
            prefix function { 
            void f(int ) {} };    \
            prefix both { 
            int i; double f() { return 0.0; } };

            namespace structures { DEFINITION(struct) }
            namespace s = structures;

            namespace classes { DEFINITION(class) }
            namespace c = classes;

            namespace unions { DEFINITION(union) }
            namespace u = unions;

            #undef DEFINITION

            /**
            上面對class、struct、union分別定義了:
            none        有數據成員,無成員函數
            data        有數據成員,沒成員函數
            function    無數據成員,有成員函數
            both        有數據成員,有成員函數
            */

            /** ------------------------------------------------------------------------ */

            void test_pointer_to_data_member() {

                
            // 一旦某個類型不是基本數據類型,就可以定義成員指針(數據成員指針,成員函數指針)。
                
            // 即使它沒有數據成員或者成員函數。

                
            // s::none 并沒有數據成員或者成員函數。
                int s::none::* p; //但是可以定義一個指向s的數據成員指針,只要類型不是void
                
            // void s::none::* p2; //error C2182: 'p2' : illegal use of type 'void'

                
            // 同時,在C++中,字面值0可以隱式轉換到任何指針類型。
                p = 0// ok
                double s::none::* p3 = 0// ok

                
            // 但是,如果某類型沒有對應類型的數據成員,就不能用數據成員指針去指向它。
                int s::data::* p4 = 0;
                p4 
            = &s::data::i;  // ok
                double s::data::* p5 = 0;
                
            // p5 = &s::data::i;
                
            // error C2440: '=' : cannot convert from 'int structures::data::* ' to 'double structures::data::* '

                (
            void)p3; (void)p5;
            }

            // 這個是比較完整的測試
            void test_pointer_to_data_member_integrate();


            /** ------------------------------------------------------------------------ */

            void test_pointer_to_member_function() {
                
            // 同理,一旦某個類型不是基本類型,就可以定義指向該類型的成員函數的指針。
                
            // 并且字面值0可以隱式轉換到該指針。

                
            int (u::none::* p1)(void)  = 0;
                
            double (u::none::* p2)(double= 0;

                
            // 如果該類型確實有匹配的成員函數,可以使用該成員函數給指針賦值。
                double (u::both::* p3 )(void= &u::both::f;
                
            void (u::function::* p4)(int= &u::function::f;

                
            // 否則不能賦值
                
            // double (u::both::* p5 )(void) = &u::function::f;
                
            //error C2440: 'initializing' : cannot convert from 'void (__thiscall unions::function::* )(int)' to 'double (__thiscall unions::both::* )(void)'
                
            // void (u::function::* p6)(int) = &u::both::f;
                
            //error C2440: 'initializing' : cannot convert from 'double (__thiscall unions::both::* )(void)' to 'void (__thiscall unions::function::* )(int)'

                (
            void)p1; (void)p2; (void)p3; (void)p4;
            }

            // 這個是比較完整的測試
            void test_pointer_to_member_function_integrate();

            /** ------------------------------------------------------------------------ */
            /**
            那么,測試一個類型是否是內建類型的“一個”方法就是
            */

            namespace SFINAE {

                
            class true_type { char dummy; true_type(); };
                
            class false_type { char dummy[2]; false_type(); };
                
            // sizeof(true_type)!=sizeof(false_type)

                template
            <class C>
                true_type is_not_buildin_type_test(
            int C::* pointer_to_data_member);

                template
            <typename T>
                false_type is_not_buildin_type_test();

                
            void test_theory() {
                    
            using namespace std;

                    
            /* 在當前名字空間下, is_not_buildin_type_test是2個函數模板的名字:
                    template<class C>
                    true_type is_not_buildin_type_test(int C::* pointer_to_data_member);
                    (以下簡稱模板1)

                    template<typename T>
                    false_type is_not_buildin_type_test();
                    (以下簡稱模板2)

                    它們相互構成重載。
                    
            */

                    cout
            <<sizeof( is_not_buildin_type_test<c::none>(0) )<<endl;
                    
            // 0 可以隱式轉化成 int c::none::* ,可以匹配模板1 (with C = c::none)

                    
            // 這里int是無關緊要的, 只要不是void就行
                    
            // 當然使用其他類型的數據成員指針 T C::* (T!=void)
                    
            // 或者成員函數指針進行測試 T (C::*)(paramter_list),也是可以的
                    
            // 只是int C::* 寫起來比較方便。

                    
            // 因為() 可以匹配任何類型的對象,
                    
            // 所以 0 也可以匹配模板2

                    
            // 又因為()處于重載選擇優先級中的最底層,所以最終匹配模板1。
                    
            // 注意,此處模板2不能使用(int),或者(T*)因為它的優先級高于(int C::*)


                    cout
            <<sizeof( is_not_buildin_type_test<double>(0) )<<endl;
                    
            // 0 不能隱式轉換成 int double::*,也就不能匹配模板1 ( with C=double )

                    
            // 但是還有一個“補救”的函數模板2,可以匹配任何類型的對象。
                    
            // 又因為SFINAE(Substitution failure is not an error)機制
                    
            // 所以對模板1的失敗的匹配并不報錯。
                }

                
            // 還有一些細節
                
            // 比如is_not_buildin_type_test并沒有實現。
                
            // 但是因為它同時也沒有被調用, 而僅僅是用sizeof測試它的返回值,所以不算錯誤。
                
            // 也防止了客戶無意調用這個函數。

                
            // 如何得知哪個is_not_buildin_type_test被重載選中?
                
            // 是通過返回值的大小不同來區分的。

                
            // 所以就需要true_type和false_type這2個東西。
                
            // 其實更合理的命名應該是small_type和big_type。
                
            // 同時,它們聲明有私有的構造函數,并且不實現,防止客戶使用這2個類。

                
            // 還因為is_not_buildin_type_test并沒有真正實現
                
            // 也就沒有真正返回true_type或者false_type
                
            // 所以沒有實現true_type和false_type的構造函數也沒關系。

                
            // 一切都因為sizeof ……

                
            /** -------------------------------------------------------------------- */
                
            /**
                將這種方法再包裝一下
                (避免客戶去使用sizeof( xxx ) == sizeof( ture_type )等等)
                就得到
                
            */

                template
            <typename T>
                
            class is_not_buildin_type {
                    is_not_buildin_type();
                
            public:
                    
            enum { value =
                        
            sizeof(true_type)==sizeof( is_not_buildin_type_test<T>(0) ) };
                };
                
            // 或者將true_type,false_type,定義為它的內嵌類型。
                
            // 同時將is_not_buildin_type_test定義為它的靜態成員函數。

                template
            <typename T>
                
            class is_not_buildin_type2 {
                    is_not_buildin_type2();

                    
            // 因為是內嵌的private,客戶不能訪問
                    
            // 所以可以隨意一點
                    typedef char small_t;
                    
            struct big_t { small_t dummy[2]; };

                    template
            <typename U>
                    
            static big_t test(void (U::*)(shortfloat) );
                    
            // 只要是成員指針就ok,無論是數據成員指針還是成員函數指針。
                    
            // 也無論類型,簽名如何。

                    template
            <typename U>
                    
            static small_t test();
                    
            // 注意補救函數現在返回small_t

                
            public:
                    
            // 但這也是無關緊要的,因為small_t和big_t只是告之哪個重載被選中的方式。
                    
            // 只要這里處理好對應就可以了。
                    enum { value= sizeof(big_t)==sizeof( test<T>(0) ) };
                };

                
            void test_wrapper() {
                    
            using namespace std;
                    cout
            <<is_not_buildin_type<c::data>::value<<endl;
                    cout
            <<is_not_buildin_type<u::both>::value<<endl;
                    cout
            <<is_not_buildin_type<float>::value<<endl;

                    cout
            <<is_not_buildin_type2<c::data>::value<<endl;
                    cout
            <<is_not_buildin_type2<u::both>::value<<endl;
                    cout
            <<is_not_buildin_type2<float>::value<<endl;
                }

                
            // 一個更完整的測試
                void test_wrapper_integrate();
            }

            /** ------------------------------------------------------------------------ */
            /**測試一個類型是否是內建類型的另一個方法,需要更少的技巧。*/

            namespace partial_specialization {

                template
            <typename T>
                
            struct is_not_buildin_type { enum { value=true }; };
                
            // T不是一個內建類型

                
            // 除非
                template<>
                
            struct is_not_buildin_type<int> { enum { value=false}; };
                
            // T是int
                template<>
                
            struct is_not_buildin_type<unsigned int> { enum { value=false}; };
                
            // T是unsigned int
                
            // .. more ..
            }

            int main()
            {
                
            using namespace std;
                test_pointer_to_data_member();
                test_pointer_to_data_member_integrate();
                test_pointer_to_member_function();
                test_pointer_to_member_function_integrate();

                cout
            <<endl;
                SFINAE::test_theory();
                cout
            <<endl;
                SFINAE::test_wrapper();
                cout
            <<endl;
                SFINAE::test_wrapper_integrate();
            }



            void test_pointer_to_data_member_integrate() {
                
            // to do
            }
            void test_pointer_to_member_function_integrate() {
                
            // to do
            }

            namespace SFINAE {
                
            void test_wrapper_integrate() {
                    
            // to do
                }
            }

            這個代碼已經能很完美的解釋了~
            今天看了C++ Templates 的15章.. 收獲頗多.. 以前的很多疑惑都比較開朗了~

            posted on 2009-03-16 23:32 Charlie 侯杰 閱讀(2663) 評論(3)  編輯 收藏 引用
            by Charlie
            久久伊人影视| 久久精品无码一区二区三区免费| 国产精久久一区二区三区| 国产A级毛片久久久精品毛片| 欧美久久精品一级c片片| 久久激情亚洲精品无码?V| 久久综合偷偷噜噜噜色| 久久ZYZ资源站无码中文动漫| 久久www免费人成精品香蕉| 亚洲欧美成人久久综合中文网 | 亚洲精品无码久久久影院相关影片| 久久只这里是精品66| 99久久国产综合精品网成人影院| 久久人做人爽一区二区三区| A级毛片无码久久精品免费| 国色天香久久久久久久小说 | 久久中文字幕精品| 久久青青草原国产精品免费 | 久久综合九色综合久99| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 成人a毛片久久免费播放| 久久精品国产亚洲网站| 久久99国产精品久久99| 91久久婷婷国产综合精品青草 | 久久亚洲欧美国产精品| 久久99热只有频精品8| 国产精品女同久久久久电影院| 少妇被又大又粗又爽毛片久久黑人| 久久精品国产99久久丝袜| 热re99久久精品国产99热| 久久久黄片| 久久久久人妻精品一区| 久久久久久久久久久免费精品| 久久久久久久亚洲精品| 久久免费的精品国产V∧| 国产精品免费久久| 久久妇女高潮几次MBA| 亚洲国产精品婷婷久久| 一本一道久久综合狠狠老| 国产成人久久精品麻豆一区| 麻豆AV一区二区三区久久|