MISRA C++:2023 Rule 15.0.1
Description
Rule Definition
Special member functions shall be provided appropriately.
Rationale
Special member functions can be provided by the user or implicitly provided by the compiler. It is not always clear which user-declared special member function suppresses which implicitly provided special member function. This rule clarifies which special member functions need to be user-provided. The rule imposes these requirements:
Avoid declaring any of the special member functions, unless necessary. This requirement eliminates the need to needlessly mimicking the compiler provided special member functions.
Classes must fall into one of these categories: unmovable, move only, and copy enabled. This table summarizes the copyability and movability of these categories:
Category Move Constructible Move Assignable Copy Constructible Copy Assignable Unmovable No No No No Move only Yes Optional No No Copy enabled Yes Optional Yes Optional This requirement enforces that copy-constructible classes are also move constructible.
If a class has nondefault, user-provided or customized copy or move operations, it must also have customized destructor. A customized destructor is a destructor that contains at least one statement that is neither a compound statement nor a null statement. For example:
This requirement enforces correct destruction of classes that handle resource.struct myClass { int i; myClass(const myClass &other) { i = other.i; } myClass(myClass &&other) { i = other.i; } ~myClass() { //Nondefault user provided destructor i = 0; } };
If a move-only class has a customized destructor, it must also have customized move constructor. If the class is move assignable, it must also have customized move assignment operator. If a copy-enbabled class has a customized destructor, then it must also have customized copy constructor. If the class is copy-assignable, it must also have customized copy assignment operator. This requirement enforces correct copy and move operations for classes that handles resources.
If an unmovable class is used as a
public
base class, then the class must have apublic virtual
destructor. If a move-only or copy-enabled class is used as apublic
base class, it must haveprotected
non-virtual
destructor. This requirement reduces the risk of slicing. This requirement does not apply to aggregate classes.
Additionally, the rule requires that all out-of-class definitions of special member functions be placed in the same file.
Polyspace Implementation
Polyspace® reports a violation if one or more of these conditions are true:
A class does not match any of the copyability and movability combinations described in the preceding table. Such a class cannot be categorized as any of unmovable, move only, or copy enabled.
Assignment operators are partially defined.
A class is copy constructible but not move constructible.
A class is copy or move assignable but not constructible.
A class has a customized copy or move operation but no customized destructor.
A class has a customized destructor but lacks customized copy or move operations.
A nonaggregate unmovable
public
base class lacks apublic virtual
destructor.A nonaggregate move-only or copy-enabled
public
base class lacks aprotected
non-virtual
destructor.An out-of-class definition of a special member function is placed in a different file than the other special member functions.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Special member functions |
Category: Required |
Version History
Introduced in R2024b