CppCoreGuidelines icon indicating copy to clipboard operation
CppCoreGuidelines copied to clipboard

Never delete a move constructor or move assignment operator

Open JordanMaples opened this issue 4 years ago • 5 comments

As quoted from @jwakely in https://github.com/microsoft/GSL/pull/842#issuecomment-673403244

Never delete a move constructor or move assignment operator. If you want moving to be equivalent to copying then do not write move ctor/assignment, and ensure there is a user-declared copy ctor or copy assignment operator or both (or a user-declared destructor) N.B. user-declared not user-provided, which means not_null(const not_null&) = default; is fine.

JordanMaples avatar Aug 26 '20 22:08 JordanMaples

A counterexample is ClonableBase from C.21: If you define or =delete any copy, move, or destructor function, define or =delete them all but I think there could be a useful guideline here.

cubbimew avatar Sep 02 '20 19:09 cubbimew

Yes, an absolute statement like "never" is never correct :-)

Maybe "never delete it unless you want to prevent moving and copying".

jwakely avatar Sep 02 '20 20:09 jwakely

Editors call: @cubbimew please create a new guideline along these lines. Thanks!

hsutter avatar Sep 03 '20 18:09 hsutter

So, it is important when we have a template member of a class and depending on who it is a move constructor or move assignment operator will be generated from that. Are there other cases where we shouldn't declare them?

KindDragon avatar Dec 14 '22 14:12 KindDragon