Compling error

These examples below certainly are not allowed, why?
1
2
a += 10 -= 50;
A - 10 += 33;
Last edited on
The first one parses as a += ( 10 -= 50); and the expression 10 -= 50 is not valid because 10 is an rvalue, and compound assignment expects an lvalue on the left.

The second one works fine for most class types that have operator-, but fails for scalars for the same reason: A-10 is an rvalue.

This compiles, for example:
1
2
3
4
5
6
7
8
9
10
11
12
struct S {
    S operator-(int) const { return *this; }
    S& operator+=(int) { return *this; }
};
int main()
{
    int a;
    S A;

    (a += 10) -= 50; // gotta add the parentheses
    A - 10 += 33;   // gotta use a type that overloads the operators
}
Last edited on
Hey Cubbi, I'm real happy for you and I'mma let you finish, but won't the operators only work in this case if they return an object (by reference or a copy, either way)?
Topic archived. No new replies allowed.