Rule A12-8-4 (required, implementation, automated)
Move constructor shall not initialize its class members and base classes using copy semantics.
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 copyinitialization for data members and base classes will have negative impact on the program’s performance, as well as it does not meet developer expectations.
Exception
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),
s(oth.s)
// Compliant by exception, std::int32_t is of scalar
// type
// Non-compliant
{
}
private:
std::int32_t x;
std::string s;
};
class C
{
public:
// ...
C(C&& oth)
: x(oth.x),
s(std::move(oth.s))
{
}
// Compliant
// Compliant
by exception
private:
std::int32_t x = 0;
std::string s = "Default string";
};
See also
SEI CERT C++ [10]: OOP11-CPP Do not copy-initialize members or base classes from a move constructor.