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

            My Links

            Blog Stats

            News

            常用鏈接

            留言簿(1)

            隨筆分類(13)

            隨筆檔案(17)

            文章檔案(1)

            相冊

            收藏夾(1)

            Friends' blog

            useful sites

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            精产国品久久一二三产区区别| 性做久久久久久久久老女人| 国产A三级久久精品| 香蕉久久夜色精品国产小说| 亚洲人成无码久久电影网站| 日韩亚洲国产综合久久久| 成人国内精品久久久久影院| 72种姿势欧美久久久久大黄蕉| 国产偷久久久精品专区| 777午夜精品久久av蜜臀| 久久婷婷五月综合色高清| 久久久久免费精品国产| 国产福利电影一区二区三区久久久久成人精品综合 | 日本精品久久久久中文字幕8| 久久AV高潮AV无码AV| 久久久久久国产精品美女| 国产精品成人99久久久久 | 久久久久亚洲AV综合波多野结衣 | 久久亚洲精品无码aⅴ大香| 亚洲?V乱码久久精品蜜桃| 色婷婷综合久久久久中文字幕 | 久久精品国产亚洲AV香蕉| 久久久精品国产sm调教网站| 久久精品人人做人人爽电影蜜月 | 伊人久久大香线蕉综合热线| 手机看片久久高清国产日韩 | 伊人久久大香线蕉综合网站| yy6080久久| 国产精品久久影院| 天天综合久久一二三区| 精品综合久久久久久888蜜芽| 国产成人99久久亚洲综合精品| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久国产高潮流白浆免费观看| 中文字幕成人精品久久不卡| 国产69精品久久久久APP下载| 热99re久久国超精品首页| 无码人妻少妇久久中文字幕蜜桃| 久久久精品一区二区三区| 亚洲精品乱码久久久久66| 性做久久久久久久久久久|