Consider the following code:
struct A {
A& add( int i ) { return *this; }
};
A& operator+=( A& a, int i ) { return a; }
void f() {
A a1 = A().add( 1 ); // expr 1
A a2 = A() += 1; // expr 2
}
Both expressions 1 and 2 are accepted by VC. Expression 2 is rejected by GCC with "No viable overloaded '+='". However such an expression is accepted for operators taking a reference to constant object:
const A& operator+=( const A& a, int i ) { return a; }
Why does GCC force this temporary to be constant in the operator context when not in the method call context, and is this correct behavior?