私の知る限り、戻る*this
ことは overloaded を書く際の標準ですoperator=
。しかし、これは右辺値を左辺値に昇格させるかもしれません!
struct Foo {
Foo& operator=(const Foo &t) { return *this; }
};
int main() {
const Foo &ref = Foo();
//Foo &ref1 = Foo(); // This line won't compile.
//But the following line compiles. Isn't it dangerous?
//Also please note that if = is not overloaded, the synthesized operator= will NOT let it compile.
Foo &ref2 = (Foo() = Foo());
return 0;
}