gpt4 book ai didi

c++ - Clang、std::shared_ptr 和 std::less/operator<

转载 作者:可可西里 更新时间:2023-11-01 17:37:14 26 4
gpt4 key购买 nike

有如下代码

#include <memory>

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

bool result = ptr0 < ptr1;
}

在用 clang (version 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<() 对 clang 中的共享指针不起作用有什么原因吗?

最佳答案

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*&&& 出现。

关于c++ - Clang、std::shared_ptr 和 std::less/operator<,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11115265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com