• <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 丑石 閱讀(426) 評論(0)  編輯 收藏 引用 所屬分類: C++ problem and solution

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            伊人色综合久久天天人手人婷| 国内精品久久久久久久涩爱| 亚洲国产成人乱码精品女人久久久不卡 | 国产激情久久久久影院| 久久综合五月丁香久久激情| 亚洲欧美成人综合久久久| 九九久久99综合一区二区| 亚洲а∨天堂久久精品| 久久99国产精品尤物| 亚洲国产高清精品线久久| 色婷婷综合久久久中文字幕 | 老男人久久青草av高清| 久久福利青草精品资源站免费| 国产精品欧美久久久久无广告| 久久综合视频网| 久久国产乱子伦精品免费午夜| 久久久久久国产精品免费无码| 欧美成人免费观看久久| 91精品国产综合久久久久久| 午夜精品久久久久久99热| 久久精品国产亚洲7777| 91久久婷婷国产综合精品青草 | 久久亚洲欧美日本精品| 久久香蕉超碰97国产精品| 久久精品国产72国产精福利| 精品人妻久久久久久888| 久久夜色精品国产噜噜亚洲a| 久久成人国产精品一区二区| 精品国产一区二区三区久久久狼| 三级三级久久三级久久| 亚洲国产成人久久一区久久| 中文字幕一区二区三区久久网站| 国产一区二区三精品久久久无广告| 色婷婷久久综合中文久久蜜桃av| 亚洲性久久久影院| 日本精品久久久久久久久免费| 久久精品国产只有精品2020| 久久久久久亚洲Av无码精品专口| 伊人久久无码中文字幕| 奇米影视7777久久精品| 无码伊人66久久大杳蕉网站谷歌|