Why is this an issue?
Copy assignment operators and move assignment operators can return anything, including void
.
However, if you decide to declare them yourself (don’t forget the "Rule-of-Zero", S4963), it is a recommended practice to return a
non-const reference to the left-operand. It allows the developer to chain the assignment operations, increasing consistency with what other types do,
and in some cases enabling writing concise code.
Noncompliant code example
class A {
public:
~A() = default;
A(A const &) = default;
A(A&&) = default;
const A& operator=(const A& other) ; // Noncompliant
A operator=(A&& other) noexcept; // Noncompliant
};
Compliant solution
class A {
public:
~A() = default;
A(A const &) = default;
A(A&&) = default;
A& operator=(const A& other);
A& operator=(A&& other) noexcept;
};
Resources