Why is the following code correct:
struct A
{
A operator+( A const& elem )
{
return *this;
}
bool operator==( A const& elem )
{
return true;
}
operator bool()
{
return true;
}
};
int main()
{
A a_1, a_2;
if( ((a_1+a_2) = a_1) )
{}
}
Here, I expected an error in the if-statement since a_1 + a_2 is an r-value. Replacing the line A a_1, a_2; by int a_1, a_2; leads to the expected error:
error: expression is not assignable
if( ((a_1+a_2) = a_1) )
~~~~~~~~~ ^
1 error generated.