2012-06-20 17 views
8

निम्नलिखित कोडबजना, std :: shared_ptr और std :: कम/ऑपरेटर <

#include <memory> 

int main() { 
    std::shared_ptr<int> ptr0(new int); 
    std::shared_ptr<int> ptr1(new int); 

    bool result = ptr0 < ptr1; 
} 

निम्न त्रुटि पैदा करता है जब बजना के साथ संकलित किया जा रहा करने के बाद (संस्करण 3.1, LLVM 3.1, डेबियन GNU/लिनक्स सिड)

/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/shared_ptr.h:364:14: error: no matching function for call to object of type 'std::less<_CT>' 
     return std::less<_CT>()(__a.get(), __b.get()); 
      ^~~~~~~~~~~~~~~~ 
foo.cpp:9:21: note: in instantiation of function template specialization 'std::operator<<int, int>' requested here 
     bool result = ptr0 < ptr1; 
         ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/stl_function.h:236:7: note: candidate function not viable: no known conversion from 'int *' to 'int *&&&' for 
     1st argument; 
     operator()(const _Tp& __x, const _Tp& __y) const 
    ^

जीसीसी (संस्करण 4.7.0) के साथ एक ही कोड को संकलित करने से कोई त्रुटि संदेश नहीं फेंकता है। क्या कोई कारण है कि ऑपरेटर <() क्लैंग में साझा पॉइंटर्स के लिए काम नहीं करता है?

+11

वाह, 'int * &&&' ... – kennytm

उत्तर

12

clang ++ और libstdC++ पूरी तरह से मेल नहीं खाता है। आप निम्नलिखित में से एक कर सकता है: libc को ++ (clang++ -stdlib=libc++ -std=c++11 ... का उपयोग करके)

  • /usr/include/c++/4.7.0/type_traits के लिए निम्न पैच लागू करें (के रूप में http://clang.llvm.org/cxx_status.html में दस्तावेज)

    • स्विच:

      Index: include/std/type_traits 
      =================================================================== 
      --- include/std/type_traits (revision 185724) 
      +++ include/std/type_traits (working copy) 
      @@ -1746,7 +1746,7 @@ 
      
          template<typename _Tp, typename _Up> 
          struct common_type<_Tp, _Up> 
      - { typedef decltype(true ? declval<_Tp>() : declval<_Up>()) type; }; 
      + { typedef typename decay<decltype(true ? declval<_Tp>() : declval<_Up>())>::type type; }; 
      
          template<typename _Tp, typename _Up, typename... _Vp> 
          struct common_type<_Tp, _Up, _Vp...> 
      

    हैं आप bits/shared_ptr.h को चेक करते हैं, आपको std::common_type मिल गया है, और क्लैंग डेवलपर्स का दावा है कि it's actually a bug of libstdc++, हालांकि मुझे अकेले libstdC++ की एक बग पर विश्वास नहीं है उपस्थित होने के लिए मौजूद गैर-मौजूद प्रकार int*&&& का कारण बन जाएगा।

  • संबंधित मुद्दे