The const-ness of an rvalue reference prevents intended move
operation
This defect occurs when a function takes a const rvalue reference as
parameter. For instance, this move constructor takes a const rvalue
reference:
class aClass {
public:
aClass (const aClass&& anotherClass);
}The const nature of the rvalue reference parameter prevents the
expected move operation.
For instance, this issue can happen when you write a move constructor by copy-paste from
a copy constructor with a const parameter, for instance:
aClass (const aClass& anotherClass);
& to &&
but forget to omit the const in the reference or the copy operations in
the constructor body. In this case, the move constructor with the const
rvalue reference compiles without errors but leads to an inefficient move constructor that
actually copies the data.Remove the const qualifier from the rvalue reference
parameter.
For instance, the move constructor in the preceding section can be rewritten as:
class aClass {
public:
aClass (aClass&& anotherClass);
}Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
| Group: Performance |
| Language: C++ |
| Default: Off |
Command-Line Syntax:
CONST_RVALUE_REFERENCE_PARAMETER |
| Impact: Low |
Const parameter values may cause
unnecessary data copies | Const return values may cause
unnecessary data copies | std::move called on an unmovable
type