Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is sort() automatically using move semantics?

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?

like image 399
johnbakers Avatar asked Apr 19 '16 18:04

johnbakers


People also ask

What is move semantics?

Move semantics allow you to define how your classes guts can be moved out and dropped in a different object when the compiler knows the object you are moving from is about to go away. The only reference I can understand: learncpp.com/cpp-tutorial/…, i.e. the original reasoning of move semantics is from smart pointers.

What is return value in C++ move semantics?

The return value is a rvalue reference to the object. Working of Move Semantics in C++ Whenever there is a need to move the contents of the objects between the objects instead of copying the contents from one object to another object, we make use of Move Semantics in C++.

Do I need a GitHub account to use move semantics?

To get benefits from move semantics you need some kind of resource (e.g. pointers to dynamically-allocated objects, file descriptors, TCP sockets, I/O streams, running threads, etc.). The source code containing the MemoryBuffer example is here (under MoveSemantics ). You do not need a Github account to download it.

How does the move assignment operator work?

The move constructor transfers ownership of a managed resource into the current object. The last missing piece is the move assignment operator. Its job is to release the old resource and acquire the new resource from its argument:


1 Answers

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).

like image 187
NathanOliver Avatar answered Oct 13 '22 00:10

NathanOliver