AutosarC++17_03-A12.8.4¶
Move constructor shall not initialize its class members and base classes using copy semantics
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Data members or base classes initialization in move constructor needs to be done with move semantics. Move construction is an optimization strategy and the copy- initialization for data members and base classes will have negative impact on the program’s performance, as well as it does not meet developer expectations.
Exceptions
In move constructor, copy initialization for data members of scalar types does not violate this rule.
See: Scalar-Types.
Example
// $Id: A12-8-4.cpp 271696 2017-03-23 09:23:09Z piotr.tanski $
#include <cstdint>
#include <string>
class A {
public:
// ...
A(A &&oth)
: x(std::move(oth.x)), // Compliant
s(std::move(oth.s)) // Compliant
{}
private:
std::int32_t x;
std::string s;
};
class B {
public:
// ...
B(B &&oth)
: x(oth.x), // Compliant by exception, std::int32_t is of scalar
// type
s(oth.s) // Non-compliant
{}
private:
std::int32_t x;
std::string s;
};
class C {
public:
// ...
C(C &&oth)
: x(oth.x), // Compliant by exception
s(std::move(oth.s)) // Compliant
{}
private:
std::int32_t x = 0;
std::string s = "Default string";
};
See Also
- SEI CERT C++ [9]: OOP11-CPP Do not copy-initialize members or base classes from a move constructor.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
move_constructor |
Move constructor shall not initialize using copy semantics. |
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
This rule has no individual options.