Reputation: 24750
isocpp.org states that:
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.
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? In other words, to get the many benefits of C++11 performance improvements, you should edit existing code to add move operations explicitly?
Further, if you are sorting, is it the container or the type inside the container, or both, that must have move operations defined?
Upvotes: 5
Views: 1044
Reputation: 180435
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?
Correct. If the class is not moveable then it will fall back to copying
In other words, to get the many benefits of C++11 performance improvements, you should edit existing code to add move operations explicitly?
If you can sure, who doesn't like more performance. Do note that depending on the class you may get 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?
The container itself is not required to be moveable. std::sort
requires that the iterator passed to it shall satisfy the requirements of ValueSwappable (17.6.3.2). and that the type returned by dereferencing the iterator shall satisfy the requirements of MoveConstructible (Table 20) and of MoveAssignable
(Table 22).
Upvotes: 8