AutosarC++17_10-A13.2.1

An assignment operator shall return a reference to “this”

Required inputs: IR

Rationale

Returning a type “T&” from an assignment operator is consistent with the C++ Standard Library.

Example
// $Id: A13-2-1.cpp 271687 2017-03-23 08:57:35Z piotr.tanski $
class A {
public:
  // ...
  A &operator=(const A &) & // Compliant
  {
    // ...
    return *this;
  }
};

class B {
public:
  // ...
  const B &operator=(const B &) & // Non-compliant - violating consistency
                                  // with standard types
  {
    // ...
    return *this;
  }
};

class C {
public:
  // ...
  C operator=(const C &) & // Non-compliant
  {
    // ...
    return *this;
  }
};

class D {
public:
  // ...
  D *operator=(const D &) & // Non-compliant
  {
    // ...
    return this;
  }
};
See Also
  • HIC++ v4.0 [8]: 13.2.2 Ensure that the return type of an overloaded binary operator matches the built-in counterparts.
  • C++ Core Guidelines [10]: F.47: Return T& from assignment operators.

Possible Messages

Key

Text

Severity

Disabled

asgn_ref_this

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

None

False

Options