class Test {
public:
int n1;
};
Test func() {
return Test();
}
int main() {
func() = Test();
}
This doesn\'t make sense to me.
There's a pretty steep learning curve as far as value categories are concerned (at least to me), but I believe you have got the terminology right on your example. So
func()
indeed returns a prvalue and from the C++ Standard par. 3.10.5 (I only have the current draft, your paragraph number may vary) we read :
An lvalue for an object is necessary in order to modify the object except that an rvalue of class type can also be used to modify its referent under certain circumstances. [Example: a member function called for an object (9.3) can modify the object. — end example]
So the assignment operator, which is a member function, as in the example mentioned in the standard is an exception to the rule, allowing rvalues to be modified.
This has spawned much criticism in the programming world, with its most extreme example this C++ FQA excerpt :
(Yes, sugar-coated death traps, you clueless cheerleaders.
X& obj=a.b().c()– oops,b()is a temporary object and c() returns a reference into it! Shouldn't have assigned that to a reference. Not many chances for a compiler warning, either.)
But in real C++ programming it has industrial applications like the named parameter idiom :
std::cout << X::create().setA(10).setB('Z') << std::endl;