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

            MyMSDN

            MyMSDN記錄開發(fā)新知道

            C++ Traits

            本來想好好寫寫C++ Traits的,剛剛接到interview的通知,先貼代碼,改天再議。

            /*
             * cpp_traits.cpp
             *
             *  Created on: 2010-4-26
             *      Author: volnet@tom.com
             */
            #include <iostream>
            // kinds of types for overloading.
            struct undefined_type {};
            struct int32_type {};
            struct int64_type {};
            
            // typedef some has_trivial_* for difference types.
            template <class T>
            struct type_traits {
                typedef undefined_type has_trivial_type;
            };
            
            // define the partial specialization functions.
            template <>
            struct type_traits<int> {
                typedef int32_type has_trivial_type;
            };
            template <>
            struct type_traits<long> {
                typedef int64_type has_trivial_type;
            };
            
            // the dispatcher method for all kinds of types.
            template <class T>
            void type_detect(T& p){
                typedef typename type_traits<T>::has_trivial_type trivial_type;
                type_detect(p, trivial_type());
            }
            
            // define the functions for dispatching.
            template <class T>
            void type_detect(T& p, undefined_type) {
                std::cout << p;
                std::cout << " // It's a undefined type, we have NOT found the dispatcher function." << std::endl;
            }
            template <class T>
            void type_detect(T& p, int32_type) {
                std::cout << p;
                std::cout << " // It's a int32" << std::endl;
            }
            template <class T>
            void type_detect(T& p, int64_type) {
                std::cout << p;
                std::cout << " // It's a int64" << std::endl;
            }
            
            int main(void) {
                int int32num = 2010;
                type_detect(int32num);
            
                long int64num = 2010L;
                type_detect(int64num);
            
                std::string str = "2010";
                type_detect(str);
            
                std::cout << "-------end of program." << std::endl;
                return EXIT_SUCCESS;
            }
            

            posted on 2010-04-26 15:31 volnet 閱讀(773) 評論(2)  編輯 收藏 引用 所屬分類: C/C++

            評論

            # re: C++ Traits 2010-06-03 15:57 brainpoint

            特化代碼寫得不夠美啊  回復(fù)  更多評論   

            # re: C++ Traits 2010-08-29 17:30 evening dresses

            template <class T>
              回復(fù)  更多評論   

            特殊功能
             
            精品久久久久久| 合区精品久久久中文字幕一区| 久久精品无码一区二区三区免费| 国产91色综合久久免费分享| 亚洲va久久久噜噜噜久久| 国色天香久久久久久久小说 | 91精品国产91久久久久久| 久久久无码精品亚洲日韩蜜臀浪潮| 亚洲精品乱码久久久久久久久久久久| 无码任你躁久久久久久| 亚洲午夜无码久久久久小说| 一本综合久久国产二区| 伊人久久大香线焦AV综合影院| 久久精品久久久久观看99水蜜桃| 99久久国产宗和精品1上映| 伊人久久大香线蕉亚洲| 欧美一区二区三区久久综合| 久久91精品国产91久久麻豆| 国产免费久久精品99久久| 欧美粉嫩小泬久久久久久久 | 国内精品综合久久久40p| 久久亚洲精品成人av无码网站| 久久精品一区二区国产| 欧美麻豆久久久久久中文| 久久亚洲精品无码VA大香大香| 嫩草伊人久久精品少妇AV| 91麻精品国产91久久久久| 亚洲国产成人久久综合一区77| 亚洲中文久久精品无码| 欧美激情精品久久久久| 日产精品久久久久久久| 国产精品一区二区久久| 伊人久久国产免费观看视频| 久久精品国产亚洲AV香蕉| 91亚洲国产成人久久精品| 国内精品九九久久精品 | 免费国产99久久久香蕉| 精品久久久无码人妻中文字幕| 久久99中文字幕久久| 亚洲AV无码1区2区久久| 日产久久强奸免费的看|