AutosarC++18_10-A18.9.3ΒΆ

The std::move shall not be used on objects declared const or const&

Required inputs: IR

Calling 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