Could anyone explain the details in terms of rvalues, lvalues, PODs, and non-PODs the reason why the first expression marked below is not ok while the secon
In my understanding both int() and A() should be rvalues, no?
Correct, the epxression T()
is always an rvalue for scalar and user-defined types T
. As long as no const
is involved, the expression T()
is a modifiable rvalue, to be more precise.
Assignment involving scalar types requires a modifiable lvalue on the left hand side of the assignment operator. Since int()
isn't an lvalue, you can't assign to int()
.
For user-defined types, assignment is a special member function, and member functions can also be called on rvalues (see §3.10 section 10). That's why A().operator=(a)
is well formed.