AutosarC++17_03-A12.8.1¶
Move and copy constructors shall only move and respectively copy base classes and data members of a class, without any side effects
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
It is expected behavior that the move/copy constructors are only used to move/copy the object of the class type and eventually set copied-from or moved-from object to a valid state.
Move and copy constructors of an object are frequently called by STL algorithms and containers, so they are not supposed to provide any performance overhead or side effects that could affect moving or copying the object.
Example
// $Id: A12-8-1.cpp 271696 2017-03-23 09:23:09Z piotr.tanski $
#include <cstdint>
#include <utility>
class A {
public:
// Implementation
A(A const &oth)
: x(oth.x) // Compliant
{}
private:
std::int32_t x;
};
class B {
public:
// Implementation
B(B &&oth)
: ptr(std::move(oth.ptr)) // Compliant
{
oth.ptr = nullptr; // Compliant - this is not a side-effect, in this
// case it is essential to leave moved-from object
// in a valid state, otherwise double deletion will
// occur.
}
~B() { delete ptr; }
private:
std::int32_t *ptr;
};
class C {
public:
// Implementation
C(C const &oth) : x(oth.x) {
// ...
x = x % 2; // Non-compliant - unrelated side-effect
}
private:
std::int32_t x;
};
See Also
- MISRA C++ 2008 [6]: Rule 12-8-1 A copy constructor shall only initialize its base classes and the nonstatic members of the class of which it is a member.
- HIC++ v4.0 [8]: 12.5.3 Ensure that a user defined move/copy constructor only moves/copies base and member objects.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
copy_ctor_with_side_effect |
Copy Constructor has side-effect |
None |
False |
move_ctor_with_side_effect |
Move Constructor has side-effect |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
report_copy_constructor¶
report_copy_constructor : bool = True
report_move_constructor¶
report_move_constructor : bool = True