• <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久久精品免费看国产免费| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 精品久久777| 成人a毛片久久免费播放| 一级a性色生活片久久无少妇一级婬片免费放| 久久九九久精品国产| 色婷婷综合久久久中文字幕| 国产精品永久久久久久久久久| 久久99久久99精品免视看动漫| 久久九九亚洲精品| 亚洲va久久久噜噜噜久久天堂| 国产99久久久国产精品~~牛| 一本色道久久综合亚洲精品| 久久国产香蕉一区精品| 99国产欧美久久久精品蜜芽| 久久无码国产专区精品| 国内精品久久久久久久涩爱| 精品无码久久久久久尤物| 久久这里的只有是精品23| 51久久夜色精品国产| 精品久久人妻av中文字幕| 久久这里有精品| 亚洲а∨天堂久久精品| 久久国产一片免费观看| 66精品综合久久久久久久| 久久香蕉一级毛片| 韩国三级大全久久网站| 99麻豆久久久国产精品免费| 粉嫩小泬无遮挡久久久久久| 精品熟女少妇av免费久久| 久久综合综合久久综合| 久久精品国产亚洲av水果派| 日产精品久久久久久久| 蜜臀av性久久久久蜜臀aⅴ麻豆| 久久精品国产亚洲AV影院| 久久WWW免费人成一看片| 久久综合狠狠综合久久| 久久精品国产99久久无毒不卡 | 狠狠精品久久久无码中文字幕| 91久久九九无码成人网站| 四虎国产精品免费久久久|