Clang, std::shared_ptr and 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、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)编译相同的代码不会引发任何错误消息。运算符<() 不适用于 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开发人员声称它实际上是libstdc++的错误,尽管我不相信单独的libstdc++的错误会导致不存在的类型int*&&&
出现。
相关文章:
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 包括"lvtocon.h",未定义对'operator<<(std::ostream&, char const*)的引用
- 无法引用该函数" std:: variant <_Types...> operator =",因为此函数已被删除
- std::vector::assign/std::vector::operator=(const&) 是否保证在"this"中重用缓冲区?
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- 为什么当我使用 std::string::operator[] 时自动变量不会成为引用?
- 与"operator<<"不匹配(操作数类型'std::ostream'
- 从 std::ostream 重载 << 运算符时,为什么编译器会给出"too many parameters for this operator function"错误?
- MSVC++ 17 std::copy 期望自定义迭代器的"operator -"
- 使用 operator[] 访问私有 std::map of unique_ptr
- 为什么 std::uniform_int_distribution<IntType>::operator() 不是 const?
- std::p riority_queue<struct with const member&> - 'operator =' 函数不可用
- 为什么我可以在临时 std::ofstream 对象上使用"operator<<"?
- 为 std::shared_ptr 实例调用 Operator()
- 错误:与'operator='不匹配(操作数类型为"std::map<int、double>::iterator
- C++ -- 错误 C2784: -- 请参阅'std::operator <<'声明
- 绑定到 std::operator== 时未解决的重载函数错误
- 在 Linux 中编译时,“与 'std::operator<<' 中的'运算符<<'不
- 将std::endl传递给std::operator<<