std::move
called on an unmovable type
std::move
used on a class type with no move constructor or move
assignment operator
Since R2020b
Description
This defect occurs when you use std::move
to move an object of a class
type that does not have a move constructor or move assignment operator.
Risk
The use of std::move
in statements such
as:
Obj objTo {std::move(objFrom)}; objTo = std::move(objFrom);
If the class is expensive to copy, the unintended copy operation can cause a loss of performance.
Fix
To make an object of type T
movable, add a move
constructor:
T (T&&);
T& operator=(T&&);
T
. If the class does not have to directly manage a resource, you can
use compiler-generated move operators using the =default
syntax, for
instance:T (T&&) = default;
Otherwise, if a move operation is not required, remove the std::move
call and directly copy the object.
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:
STD_MOVE_UNMOVABLE_TYPE |
Impact: Medium |
Version History
Introduced in R2020b
See Also
Find defects
(-checkers)
| Const std::move input may cause a more
expensive object copy
| Const rvalue reference parameter may
cause unnecessary data copies
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)