Search code examples
c++copy-assignment

Is there ever a valid reason to not return *this from copy assignment operator?


Let foo be a struct or class with a copy assignment operator:

struct foo {
    foo &operator=(const foo &);  // or with some other return type?
};

Is there ever a sensible reason to return anything other than *this from the operator=()? Using it for something unrelated to assignment doesn't qualify as sensible.


Solution

  • The example from the standard is std::atomic. It returns the assigned value. If it returned reference, then reading through it might yield different result.