Can anybody explain why this code compiles:
typedef struct longlong
{
unsigned long low;
long high;
}
longlong;
typedef longlong Foo;
struct FooStr
This works because longlong
is a class, and as such =
is longlong::operator =
, the implicitly-defined assignment operator. And you can call member functions on temporaries as long as they're not qualified with &
(the default operator =
is unqualified).
To restrict the assignment operator to lvalues, you can explicitly default it with an additional ref-qualifier:
struct longlong
{
longlong &operator = (longlong const &) & = default;
// ^
// ...
};