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

            linux&c++ R&D

            programing is a pleasure!

            how to avoid instantiation of template operator?

            problem:
            I need to prevent given operator from being generated
            if underlying operation can't be compiled. Basically, this operator==:
            template<class T> class accessor 
                 typedef accessor
            <T> Self; 
            public
                 template
            <class C> friend inline bool operator==(Self l, C& r) 
                 
            return l.m_ref == r; } 
            }
             

            should not be instantiated if 'l.m_ref == r' fails to compile due to, for example, absence of related operator==.
            solution:

            template <bool Enable, typename T = void> 
            struct enable_if 

              typedef T type; 
            }

            template 
            <typename T> 
            struct enable_if
            <false, T> 
            }

            namespace Details 

            struct Shim 

              Shim(); 
            // Can be implicitely constructed by 
                         
            // everything (Not defined). 
            }

            struct NoMatchType 

            }

            // Only declared, never defined: 
            NoMatchType operator==(const Shim&const Shim&); 
            template 
            <typename T, typename U> 
            struct IsEqualComparableImpl 

            private
                
            static const T& t(); 
                
            static const U& u(); 
                
            static char Check(bool); // Expected result 
                static char (&Check(const NoMatchType&))[2]; // Fallback 
            public
                
            static const bool value = sizeof(Check(t() == u())) == 1
            }

            }
             

            template 
            <typename T, typename U> 
            struct IsEqualComparable 

              
            static const bool value = 
                Details::IsEqualComparableImpl
            <T, U>::value; 
            }

            //test example
            template<class T> class accessor 
               typedef accessor
            <T> Self; 
            public
               accessor() : m_ref() 
            {} 
               template
            <class C> friend   inline   typename enable_if<IsEqualComparable<T, C>::value, bool>::type 
             operator
            ==(Self l, C& r) 
                 
            return l.m_ref == r; 
               }
             
            private
              T m_ref; 
            }

            class C{}
            class D {}
            bool operator
            ==(D, D) return true; } 
            bool test() 

              accessor
            <D> a1; 
              D d; 
              bool b 
            = a1 == d; // OK 
              accessor<int> a; 
              C c; 
              
            return a == c; // Error 
            }
             
            int main() 
              
            return test(); 
            }
             


            http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/3aa0bd9ea133b967/9ca97b1308becafe#9ca97b1308becafe

            posted on 2007-04-17 23:42 丑石 閱讀(419) 評論(0)  編輯 收藏 引用 所屬分類: C++ problem and solution

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            2021国产精品午夜久久| 亚洲精品无码成人片久久| 亚洲欧美一级久久精品| 精品999久久久久久中文字幕| 久久精品国产男包| 一本综合久久国产二区| 免费久久人人爽人人爽av| 久久亚洲精精品中文字幕| 久久久久久无码国产精品中文字幕| 久久久久国产亚洲AV麻豆| 久久婷婷五月综合97色直播| 久久精品国产亚洲AV无码偷窥| 区亚洲欧美一级久久精品亚洲精品成人网久久久久| 久久国产成人| 精品久久久久香蕉网| 综合网日日天干夜夜久久| 大香网伊人久久综合网2020| 亚洲人成伊人成综合网久久久 | 久久久久久午夜成人影院| 久久天天躁狠狠躁夜夜躁2014| 99久久精品国产高清一区二区| 青青草原综合久久| 97久久精品无码一区二区天美| 久久久精品久久久久久 | 欧美久久一级内射wwwwww.| 久久大香香蕉国产| 思思久久好好热精品国产| 国产免费久久久久久无码| 国产一区二区精品久久| 色欲久久久天天天综合网| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 国产亚州精品女人久久久久久 | 国产成人精品久久二区二区| 精品人妻伦九区久久AAA片69| 精品无码人妻久久久久久| 久久婷婷久久一区二区三区| 久久66热人妻偷产精品9| 久久天堂AV综合合色蜜桃网 | 久久久久久久综合狠狠综合| 欧美精品丝袜久久久中文字幕| 久久国产免费观看精品3|