cpp_weekly icon indicating copy to clipboard operation
cpp_weekly copied to clipboard

Do not disable the move and copy methods

Open aminya opened this issue 2 years ago • 1 comments

Channel

C++ Weekly

Topics

It can have title like: "Do not disable the move and copy methods".

Context: I have used some C++ libraries that disable the move/copy methods for their structs while it is totally unnecessary. This has many disadvantages:

  • I cannot store these structs inside std containers (like tuple)
  • I cannot break my code into smaller functions, because I cannot return them in a tuple -> (I had to workaround this by using multiline macros instead of functions!!)
  • ...

This feature should be only used for the core types like "unique_ptr`, but because C++ allows this for user-level types, some library authors do this in the name of optimization to prevent accidental copy/moves without knowing all the implications of it.

Length

5-10 min

aminya avatar Aug 27 '22 21:08 aminya

I always disable (delete) copy/move ctors/assignments when I haven't implemented them yet or don't really plan to because I don't expect the object to be copied, to prevent accidental copies. Not sure if this is the best thing to do (maybe a static assertion yelling "TODO" would be better?)

reedhedges avatar Sep 12 '22 18:09 reedhedges