Rule A13-2-1 (required, implementation, automated)

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

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 [9]: 13.2.2 Ensure that the return type of an overloaded binary operator matches the built-in counterparts. C++ Core Guidelines [11]: F.47: Return T& from assignment operators.