c++ - 应该使用我的 `swap` 重载吗?这是 libstdc++ (GCC) 错误吗?

标签 c++ gcc clang std

考虑以下代码:

#include <algorithm>
#include <iostream>
#include <vector>

struct A {
   int val;

   bool operator<(const A& other) const {
      std::cout << "operator\n";
      return val < other.val;
   }
};

void swap(A& a, A& b) {
   std::cout << "foo\n";
   std::swap(a.val, b.val);
}

int main()
{
   std::vector<A> a(2);
   a[0].val = 10;
   a[1].val = -1;

   std::sort(a.begin(), a.end());
}

C++11 的 std::sortValueSwappable 要求放在迭代器参数、移动语义上,仅此而已,这意味着 std::sort如果需要移动元素,则“保证”执行交换。和 17.6.3.2/3建议在这种情况下绝对应该选择我的过载。

  • 这是正确的吗?

clang 3.1 SVN 的 libc++ 选择我的 swap (也就是说,我看到“foo”); GCC 4.6.3 的 libstdc++ 没有。

  • 这是 GCC 错误吗(假设我的标准解释是正确的)?还是我遗漏了什么?

最佳答案

C++11's std::sort places ValueSwappable requirements on the iterator arguments, move semantics and nothing else, implying that std::sort is "guaranteed" to perform a swap if elements need to be moved around.

我看不到这种保证。谁说 std::sort 不能使用移动语义而不是交换?事实上,在浏览了逐字规范的标准之后,我相信这正是发生的事情:

Requires: RandomAccessIterator shall satisfy the requirements of ValueSwappable (17.6.3.2). The type of *first shall satisfy the requirements of MoveConstructible (Table 20) and of MoveAssignable (Table 22).

请注意,迭代器 应该是ValueSwappable,而不是它们指向的元素。

关于c++ - 应该使用我的 `swap` 重载吗?这是 libstdc++ (GCC) 错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9539014/

相关文章:

c++ - clang 交错源代码和程序集

clang - LLVM 如何通过运行时库调用转换 OpenMP 多线程代码?

c++ - 是否可以使用脚本或程序强制从磁盘读取 protected 文件?

c++ - "immutable"具有非平凡构造函数的结构

c - 了解基址指针和堆栈指针 : In Context with gcc Output

c - 为什么编译器版本出现在我的 ELF 可执行文件中?

assembly - 'sp' 在 asm clobber 列表中意味着什么?

关于封装的 C++ 迭代节点

c++ - 一种在再次打开文件之前知道文件当前是否打开的方法?

C代码: Calling function in main