据我所知,返回*this
是写作超载的常态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;
}