MISRA C++:2023 Rule 15.0.2
User-provided copy and move member functions of a class should have appropriate signatures
Since R2024b
Description
Rule Definition
User-provided copy and move member functions of a class should have appropriate signatures.
Rationale
This table lists the suggested signatures of user-provided copy and move
member functions for a class X
, along with an
explanation of each function signature.
Function Signature | Explanation |
---|---|
Copy constructor
| The copy constructor takes a
The |
Move constructor
| The move constructor takes an rvalue reference to
another instance ( The constructor is declared
as |
Copy assignment operator
| The copy assignment operator takes a
The function
returns an lvalue reference to the current object
( The reference qualifier
after the parentheses ( |
Move assignment operator
| The move assignment operator takes an rvalue
reference to another instance ( The function returns an lvalue reference
to the current object ( The reference qualifier after the
parentheses ( The assignment operator is declared as
|
The coding rule allows the addition of these keywords to the signatures of user-provided copy and move member functions:
constexpr
— Add to the signature any of the constructors or assignment operators.explicit
— Add to the signature of the copy or move constructors.noexcept
— Add to the signature of the copy operations. Thenoexcept
can be conditional.
If you do not intend to use the result of an assignment operation, you can declare copy
and move assignment operators with a return type of void
.
Polyspace Implementation
The coding rule checker reports a violation if there is a mismatch between
the actual and expected signatures of user-provided move and copy member
functions. If there is more than one mismatch, Polyspace reports only
one violation but shows one message per mismatch. For example, if the
signature of a copy assignment operator uses the wrong return type and
is missing the const
qualifier, Polyspace reports one
violation with two messages.
If you declare a noncompliant move or copy function and then define it elsewhere, Polyspace® reports the violation for the declaration.
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: Advisory |
Version History
Introduced in R2024b