At 3.10/10, the standard says:
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
This seems to be accepted:
struct T {
int x;
};
int main() {
T().x = 3;
}
I am slightly surprised that this works, because IIRC the LHS of op=
must be an lvalue, yet the following implies that even T().x is an rvalue:
struct T {
int x;
};
void f(int& x) {
x = 3;
}
int main() {
f(T().x);
}
Edit: As of 4.6, GCC does warn about T().x = 3
: error: using temporary as lvalue
.
I can't think of any other way to modify a class object other than through data member access or member function calls. So, I'm going to say... you can't.