AutosarC++19_03-A18.9.3ΒΆ
The std::move shall not be used on objects declared const or const&
Required inputs: IR
std::move on a const reference or const object has no effect. The
std::move function returns an rvalue reference, but since the object is const,
the rvalue reference still refers to const data. Move semantics require non-const data that
can be modified. Using std::move on const objects is misleading and indicates
a logic error.
Bad code (std::move on const):
void Process(const MyClass& obj) {
MyClass copy = std::move(obj); // ERROR: obj is const
// std::move does nothing; copy constructor called, not move constructor
}
class Handler {
const int data_;
public:
void Send() {
Transfer(std::move(data_)); // ERROR: data_ is const member
// No move happens; copy semantics used instead
}
};
Good code (remove const or don't use std::move):
void Process(MyClass& obj) { // Non-const reference
MyClass copy = std::move(obj); // OK: obj can be moved
}
void Process(const MyClass& obj) {
MyClass copy = obj; // OK: just copy, don't pretend to move
}
class Handler {
int data_; // Non-const member
public:
void Send() {
Transfer(std::move(data_)); // OK: data_ is non-const, can be moved
}
};
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
std_move_const |
Call to std::move with argument declared const/const&. |
None |
False |
Options
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.