Main Content

Move operation on const object

std::move function is called with object declared const or const&

Since R2020a

Description

This defect occurs when the std::move function is called with an object declared const or const&.

Risk

For objects declared const or const&, unlike what you might expect, the copy constructor is called instead of the move constructor.

Fix

Avoid calling the std::move function on const objects. If you want to perform a move operation, cast the const object to a non-const one and then move the non-const object.

Note that this issue might also trigger the checker Const std::move input may cause a more expensive object copy. If you decide to justify the issue, you can use the same justification for both results.

Result Information

Group: Programming
Language: C++
Default: On for handwritten code, off for generated code
Command-Line Syntax: MOVE_CONST_OBJECT
Impact: High

Version History

Introduced in R2020a