0
votes

A user-defined move constructor and move assignment operator could have been omitted in all examples explaining move semantics I have seen so far because compilers generated ones will do the job.

The answers I found in stackoverflow will not go beyond the "Rule of three" (Rule of five) - so if a class defines any of the following then it should probably explicitly define all five.

But for copy constructor and all other members the reason is obvious and an example could be written easily to show what issues can occur if user-defined copy constructor doesn't exist.

In wikipedia we can find:

A user-defined copy constructor is generally needed when an object owns pointers or non-shareable references, such as to a file.

So the question is if there are examples that will show if user-defined move constructor or move assignment operator are really needed or I can assume that in 99% of cases compiler generated ones will be enough.

1
If any of the copy operations is declared, then none of the move operation will be generated. Therefore you will have to define them manually as least as default.mcserep
As you can read in rule_of_three/five/zero, "Unlike Rule of Three, failing to provide move constructor and move assignment is usually not an error, but a missed optimization opportunity."Jarod42

1 Answers

0
votes

I guess an example of necessary customized move operation is case of a class consisting of a container and some pointer(s) to its contents; say, a circular buffer with its head and tail. But true, that's not that common to write.