考虑以下代码:
#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::sort
将 ValueSwappable 要求放在迭代器参数、移动语义上,仅此而已,这意味着 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 thatstd::sort
is "guaranteed" to perform a swap if elements need to be moved around.
我看不到这种保证。谁说 std::sort
不能使用移动语义而不是交换?事实上,在浏览了逐字规范的标准之后,我相信这正是发生的事情:
Requires:
RandomAccessIterator
shall satisfy the requirements ofValueSwappable
(17.6.3.2). The type of*first
shall satisfy the requirements ofMoveConstructible
(Table 20) and ofMoveAssignable
(Table 22).
请注意,迭代器 应该是ValueSwappable
,而不是它们指向的元素。
关于c++ - 应该使用我的 `swap` 重载吗?这是 libstdc++ (GCC) 错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9539014/