Skip to content

C.21 appears to mandate the Rule of Five -- is that desirable? #2335

Description

@hsutter

See also #2164 where this issue is creating false positives in Guidelines checkers such as clang-tidy.

Problem

C.21 mandates the Rule of Five:

C.21: If you define or =delete any copy, move, or destructor function, define or =delete them all

[...]

Enforcement

(Simple) A class should have a declaration (even a =delete one) for either all or none of the copy/move/destructor functions.

Is that too strong?

  • Yes, this will catch some errors, as noted: "to avoid unwanted effects like turning all potential moves into more expensive copies, or making a class move-only." That's good.
  • But this will also require programmers to write redundant boilerplate for classes that don't need it, such as classes that have user-written copy operations where move cannot be more efficient than copy and the programmer knows that writing only copy will cause move to be treated as copy for their type and know that's okay. Programmers hate being told to make code changes that they know don't change meaning just to satisfy a tool, and it's the kind of thing that causes them to disable a rule as noisy / low-value.

Note: The current rule and Enforcement would flag every pre-C++11 class with a user-defined copy operation.

One potential suggestion

The main noise seems to come from requiring explicit move operations. One change that could address #2164 and pre-C++11 types and most of the required-boilerplate cases would be to not require writing out move operations. Something like this:

C.21: If you define or =delete any copy, move, or destructor function, define or =delete all copy and destructor operations

[...]

Enforcement

(Simple) A class should have a declaration (even a =delete one) for all copy/destructor functions if any copy/move/destructor function is user-declared.

We could also add a note along the lines of "and if you write a copy operation check whether you should also write a more-efficient move operation, to avoid turning all potential moves into more expensive copies" -- as a Note here, or as a separate guideline.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions