Const std::move
input may cause a more expensive object
copy
Const std::move
input cannot be moved and results in more
expensive copy operation
Since R2020b
Description
This defect occurs when you use std::move
on a const
object, resulting in a more expensive copy operation.
The checker raises a violation only for class types with a nontrivial copy operation and a move operation.
Risk
A const
object cannot be modified and therefore cannot be moved. An
std::move
on a const
object silently falls back to a
copy operation without compilation errors. Your code might suffer from poorer performance
without you noticing the issue.
Fix
Remove the const
qualifier from the object being moved.
If you want a copy operation instead, remove the redundant std::move
call.
Note that this issue also triggers the checker Move operation on const
object
, which applies to all move operations on const
objects irrespective of whether the class type has a move operation and a nontrivial copy
operation. If you decide to justify the issue, you can use the same justification for both
results.
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:
EXPENSIVE_STD_MOVE_CONST_OBJECT |
Impact: Medium |
Version History
Introduced in R2020b
See Also
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)