Clang,std :: shared_ptr和std :: less / operator< [英] Clang, std::shared_ptr and std::less/operator<
问题描述
拥有以下代码
#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,LLVM 3.1)编译时,会产生以下错误:
}
$ b < Debian GNU / Linux Sid)
/ usr / bin /../ lib / gcc / x86_64-linux-gnu / ../../../../include/c++/4.7/bits/shared_ptr.h:364:14:error:没有匹配的函数调用类型'std :: less< _CT>'$ b的对象$ b return std :: less< _CT>()(__ a.get(),__b.get());
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ int>'request 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:注意:候选函数不可行:对于
,没有从'int *'到'int *&&&'的已知转换。
operator()(const _Tp& __x,const _Tp& __y)const
^
使用GCC(版本4.7.0)编译相同的代码不会抛出任何错误消息。有没有理由为什么操作符<()不适用于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 ):索引:include / std / type_traits
= ==================================================== ==============
--- include / std / type_traits(revision 185724)
+++ include / std / type_traits(working copy)
@@ -1746,7 +1746,7 @@
模板< typename _Tp,typename _Up>
struct common_type< _Tp,_Up>
- {typedef decltype(true?declval< _Tp>():declval< _Up>())type; };
+ {typedef typename decay< decltype(true?declat< _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开发者声称它实际上是libstdc ++的错误,虽然我不相信libstdc ++的错误会导致不存在的类型 int *&&&&
。 Having the following code
#include <memory>
int main() {
std::shared_ptr<int> ptr0( new int );
std::shared_ptr<int> ptr1( new int );
bool result = ptr0 < ptr1;
}
produces the following error when being compiled with 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
^
Compiling the same code with GCC (version 4.7.0) doesn't throw any error messages. Is there a reason why operator<() doesn't work for shared pointers in clang?
clang++ and libstdc++ doesn't match perfectly yet. You could do one of the followings:
- Switch to libc++ (by using
clang++ -stdlib=libc++ -std=c++11 ...
) Apply the following patch to
/usr/include/c++/4.7.0/type_traits
(as documented in 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...>
If you check bits/shared_ptr.h
you did find a std::common_type
, and the clang developers claim that it's actually a bug of libstdc++, although I don't believe a bug of libstdc++ alone would cause the non-existent type int*&&&
to appear.
这篇关于Clang,std :: shared_ptr和std :: less / operator<的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!