sort() 是否自动使用移动语义?

Is sort() automatically using move semantics?

isocpp.org 表示:

move-based std::sort() and std::set::insert() have been measured to be 15 times faster than copy based versions[...] if your type has a move operation, you gain the performance benefits automatically from the standard algorithms.

这是否意味着如果您在没有移动构造函数或移动赋值运算符的用户定义类型上调用 sort(),那么就不会使用移动语义?换句话说,要获得 C++11 性能改进的诸多好处,您应该编辑现有代码以显式添加移动操作吗?

此外,如果您正在排序,是容器还是容器内的类型,或两者都必须定义移动操作?

Does this mean that if you call sort() on a user-defined type that has no move constructor or move assignment operator, then there are no move semantics used?

正确。如果 class 不可移动,那么它将回退到复制

In other words, to get the many benefits of C++11 performance improvements, you should edit existing code to add move operations explicitly?

如果可以肯定的话,谁不喜欢更高的性能。请注意,根据 class 你可能会得到 automatically generated move operations.

Further, if you are sorting, is it the container or the type inside the container, or both, that must have move operations defined?

容器本身不需要是可移动的。 std::sort 要求传递给它的迭代器 应满足 ValueSwappable (17.6.3.2). 的要求,并且通过取消引用迭代器返回的类型 应满足MoveConstructible (Table 20) 和 MoveAssignable 的要求 (Table 22).