c++ operation precedence bug in g++ compiler vs clang++ compiler

52 views Asked by At

the output of the code below are different between g++ and clang++ ! I think clang++ is correct, what do you think?

g++ output: 11 4 13 7

clang++ output: 11 4 11 8

#include <iostream>
struct A
{ 
    int a; 
    A(int x): a{x}{}
    A  operator ++(int){ A temp = *this; ++a; return temp; }
    A& operator *(int x){ a*=x; return *this; }
    A& operator +(A& x){ a+=x.a; return *this; }
    operator int(){ return a; }
};
int main() 
{
    int myINT{3};
    int y = myINT++ + myINT * 2;
    std::cout << y << " " << myINT << '\n';

    A myA{3};
    y = myA++ + myA * 2;
    std::cout << y << " " << myA << '\n';

    return 0;
}
0

There are 0 answers