【问题标题】:Clang, std::shared_ptr and std::less/operator<Clang、std::shared_ptr 和 std::less/operator<
【发布时间】:2012-06-20 08:04:25
【问题描述】:

有以下代码

#include <memory>

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

    bool result = ptr0 < ptr1;
}

使用 clang(版本 3.1,LLVM 3.1,Debian GNU/Linux Sid)编译时产生以下错误

/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
      ^

使用 GCC(4.7.0 版)编译相同的代码不会引发任何错误消息。为什么 operator

【问题讨论】:

  • 哇,int *&amp;&amp;&amp;...

标签: c++ c++11 clang


【解决方案1】:

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,并且clang 开发人员声称it's actually a bug of libstdc++,尽管我不相信仅libstdc++ 的错误会导致不存在的类型int*&amp;&amp;&amp; 出现.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    相关资源
    最近更新 更多