• <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技術(shù)實現(xiàn)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        有數(shù)據(jù)成員,無成員函數(shù)
            data        有數(shù)據(jù)成員,沒成員函數(shù)
            function    無數(shù)據(jù)成員,有成員函數(shù)
            both        有數(shù)據(jù)成員,有成員函數(shù)
            */

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

            void test_pointer_to_data_member() {

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

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

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

                
            // 但是,如果某類型沒有對應(yīng)類型的數(shù)據(jù)成員,就不能用數(shù)據(jù)成員指針去指向它。
                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() {
                
            // 同理,一旦某個類型不是基本類型,就可以定義指向該類型的成員函數(shù)的指針。
                
            // 并且字面值0可以隱式轉(zhuǎn)換到該指針。

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

                
            // 如果該類型確實有匹配的成員函數(shù),可以使用該成員函數(shù)給指針賦值。
                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();

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

            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個函數(shù)模板的名字:
                    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)

                    它們相互構(gòu)成重載。
                    
            */

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

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

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

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


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

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

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

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

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

                
            // 還因為is_not_buildin_type_test并沒有真正實現(xiàn)
                
            // 也就沒有真正返回true_type或者false_type
                
            // 所以沒有實現(xiàn)true_type和false_type的構(gòu)造函數(shù)也沒關(guān)系。

                
            // 一切都因為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,定義為它的內(nèi)嵌類型。
                
            // 同時將is_not_buildin_type_test定義為它的靜態(tài)成員函數(shù)。

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

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

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

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

                
            public:
                    
            // 但這也是無關(guān)緊要的,因為small_t和big_t只是告之哪個重載被選中的方式。
                    
            // 只要這里處理好對應(yīng)就可以了。
                    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();
            }

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

            namespace partial_specialization {

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

                
            // 除非
                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
                }
            }

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

            posted on 2009-03-16 23:32 Charlie 侯杰 閱讀(2665) 評論(3)  編輯 收藏 引用

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


            by Charlie
            国产精品午夜久久| 久久久久久久久久久精品尤物 | 久久婷婷国产剧情内射白浆 | 色婷婷综合久久久久中文 | 国产精品久久国产精麻豆99网站| 久久久噜噜噜久久中文福利| 久久99国产精品99久久| 青青草国产成人久久91网| 中文字幕无码久久人妻| 久久永久免费人妻精品下载| 国产精品嫩草影院久久| 人妻丰满?V无码久久不卡| 久久国产精品99国产精| 久久久久久噜噜精品免费直播| 99久久精品免费看国产一区二区三区 | 日韩人妻无码精品久久久不卡| 91久久婷婷国产综合精品青草| 久久久久人妻精品一区三寸蜜桃 | 久久99国产一区二区三区| 久久久久高潮综合影院| 九九99精品久久久久久| 亚洲午夜久久久久久久久久| 精品久久久久久无码人妻蜜桃| 99久久精品免费看国产一区二区三区 | 99久久精品免费看国产一区二区三区| 一级女性全黄久久生活片免费| 国产精品美女久久久| 久久天天躁狠狠躁夜夜avapp| 青青青国产精品国产精品久久久久 | 久久这里只有精品首页| 久久偷看各类wc女厕嘘嘘| 国产成人综合久久精品红| 狠狠精品久久久无码中文字幕 | 久久无码人妻一区二区三区午夜| 久久久久久免费视频| 青春久久| 亚洲日本va午夜中文字幕久久| 久久一本综合| 亚洲第一永久AV网站久久精品男人的天堂AV | AV色综合久久天堂AV色综合在| 亚洲国产小视频精品久久久三级|