AutosarC++18_03-A12.8.4ΒΆ

Move constructor shall not initialize its class members and base classes using copy semantics

Required inputs: IR

A move constructor should use move semantics to transfer ownership of resources from the source to the destination, not copy semantics. Using copy operations in a move constructor defeats the purpose of move semantics (efficient resource transfer) and can lead to unnecessary allocation or copying, degrading performance. Move constructors should use std::move to explicitly transfer resources.
Bad code (move constructor using copy semantics):
class Data {
    std::string data_;
public:
    Data(Data&& other) {
        data_ = other.data_; // ERROR: copy ctor called instead of move ctor
    }
};
Good code (move constructor using move semantics):
class Data {
    std::string data_;
public:
    Data(Data&& other) {
        data_ = std::move(other.data_); // OK: transfer ownership
    }
};

class Container {
    std::vector<int> items_;
public:
    Container(Container&& other) noexcept
        : items_(std::move(other.items_)) {}  // OK: move semantics for members
};

Possible Messages

Key

Text

Severity

Disabled

move_constructor

Move constructor shall not initialize using copy semantics.

None

False

Options