AUTOSAR C++14 Rule A12-8-1
Move and copy constructors shall move and respectively copy base classes and data members of a class, without any side effects
Since R2021a
Description
Rule Definition
Move and copy constructors shall move and respectively copy base classes and data members of a class, without any side effects.
Rationale
The expected behavior of move and copy constructors is:
They move or copy the base classes and data members.
The move constructor sets the source object into a valid state.
Authoring move or copy constructors that have additional side effects might cause these issues:
Performance: Move and copy constructors are frequently called by standard template library (STL) algorithms and containers. Performance overhead in these constructors caused by side effects can accumulate and affect the performance of your code.
Unexpected behavior: Because compilers might omit calls to copy constructors to optimize the code, the number of times a copy constructor might be invoked is indeterminate. As a result, the side effects of a copy constructor might produce unexpected behavior.
Polyspace Implementation
In the body of a copy or move constructor, Polyspace® does not flag these operations:
Copy or move assignments.
Relational or comparison operations.
Modification of the source object in a move operation.
Calls to the function
std::swap
or equivalent user-definednoexceot
swap functions. Polyspace identifies functions that these signatures as swap functions:void T::swap(T&)
orvoid [N::]swap(T&, T&)
. The first signature represents a member function of classT
that takes one argument. The second signature represents a nonmember or static function in the namespaceN
that takes two arguments. The nameswap
can be case-insensitive and prefixed or postfixed by underscores.Assignment and modification of
static
variables.
Polyspace flags any other operations in a copy or move constructor as unwanted side
effect. For instance, a call to a user-defined swap function is considered an unwanted side
effect if the swap function is not noexcept
. For a similar rule on copy
and move assignment operator, see AUTOSAR C++14 Rule A6-2-1
.
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, Automated |
Version History
Introduced in R2021a