Can anybody explain why this code compiles:
typedef struct longlong
{
unsigned long low;
long high;
}
longlong;
typedef longlong Foo;
struct FooStr
f.GetBar() = m2; // Here I'd expect an error such as // "error: lvalue required as left operand of assignment"
Your expectation is in error. This rule only applies to objects of built-in type.
[C++14: 3.10/5]:
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 ]
Recall that your =
here is actually a call to operator=
, which is a function.