AutosarC++19_03-A13.2.1

An assignment operator shall return a reference to “this”

Required inputs: IR

Assignment operators should return a reference to the object being assigned to (typically "*this"). This allows assignment to be used in expressions like a = b = c (chained assignment). Returning void or other types prevents chaining and breaks standard assignment semantics that users expect.
Bad code (assignment not returning reference to this):
class MyInt {
    int value_;
public:
    void operator=(const MyInt& other) {  // ERROR: returns void
        value_ = other.value_;
    }
};

void Use() {
    MyInt a, b, c;
    a = b = c;  // Compile error: operator= returns void
}
Good code (assignment returning reference to this):
class MyInt {
    int value_;
public:
    MyInt& operator=(const MyInt& other) {  // OK: returns reference to this
        if (this != &other) {
            value_ = other.value_;
        }
        return *this;
    }
};

void Use() {
    MyInt a, b, c;
    a = b = c;  // OK: chaining works
}

Possible Messages

Key

Text

Severity

Disabled

asgn_ref_this

An assignment operator shall return a reference to “this”.

None

False

Options