Const rvalue reference parameter may cause unnecessary data copies
The const
-ness of an rvalue reference prevents intended move
operation
Since R2021a
Description
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); }
Risk
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.Fix
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.
Examples
Result Information
Group: Performance |
Language: C++ |
Default: Off |
Command-Line Syntax:
CONST_RVALUE_REFERENCE_PARAMETER |
Impact: Low |
Version History
Introduced in R2021a
See Also
Find defects
(-checkers)
| Const parameter values may cause
unnecessary data copies
| Const return values may cause
unnecessary data copies
| std::move called on an unmovable
type
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)