#include<iostream>
int& f(){
static int x = 0;
x++;
return x;
}
int main(){
f() += 1; //A
f() = f() + 1; //B
std::cout << f();
}
The above code outputs 6 on gcc and 5 on MSVC. Now when I modify A
and B
to f()=f()
I get 5 on both compilers. What is the big deal here? Is the behavior undefined. If yes , why?