Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the "correct" way to write the Copy/Move/operator= trio in C++11?

Tags:

c++

c++11

At this point, writing the copy constructor and assignment operator pair is well-defined; a quick search will lead you to plenty of hits on how to properly code these.

Now that the move constructor has entered the mix, is there a new "best" way?

like image 813
moswald Avatar asked Feb 22 '12 21:02

moswald


1 Answers

Preferably, they'll just be = default;, since the member types should be of resource managing types that hide the move details from you, like std::unique_ptr. Only the implementors of those "low level" types should bother with dealing with that.

Remember that you only need to bother with move semantics if you're holding an external (to your object) resource. It's completely useless for "flat" types.

like image 185
Xeo Avatar answered Sep 18 '22 14:09

Xeo