Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 887
Sami Liedes Avatar asked May 20 '15 21:05

Sami Liedes


1 Answers

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.

like image 185
zch Avatar answered Sep 20 '22 14:09

zch