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

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品国产2020| 天天爽天天狠久久久综合麻豆| 国产亚洲精品美女久久久| 一本色道久久综合| 久久无码人妻一区二区三区午夜| 亚洲综合伊人久久大杳蕉| 狠狠色丁香久久综合婷婷| 亚洲国产成人精品91久久久 | 亚洲国产成人久久精品99| 99久久无色码中文字幕人妻| 久久国产精品久久精品国产| 亚洲日韩欧美一区久久久久我 | 久久精品亚洲欧美日韩久久| 久久久久久国产精品无码下载| 好久久免费视频高清| 久久精品人人做人人爽电影| 九九精品99久久久香蕉| 国产免费久久精品99re丫y| 国产精品久久99| 性欧美丰满熟妇XXXX性久久久| 久久久国产精华液| 久久国产免费观看精品| 久久精品国产亚洲AV高清热| 国产欧美久久久精品影院| 久久久久免费视频| 精品无码久久久久久久动漫| 久久精品国产99久久久| 中文字幕久久精品无码| 久久亚洲日韩看片无码| 青青草国产97免久久费观看| 精品国产91久久久久久久a| 2020最新久久久视精品爱| 精品国产91久久久久久久| 国产∨亚洲V天堂无码久久久| 日日噜噜夜夜狠狠久久丁香五月| 2021国产精品午夜久久| 久久综合色老色| 狠狠综合久久综合88亚洲| 国产A三级久久精品| 久久受www免费人成_看片中文| 中文字幕久久亚洲一区|