#include<iostream>
int& f(){
static int x = 0;
x++;
return x;
}
int main(){
f() += 1;
f() = f() + 1;
std::cout << f();
}
The above code outputs 6 on gcc and 5 on MSVC. Now, when I change Aand Bon f()=f(), I get 5 in both compilers. What is the matter here? This behavior is undefined. If so, why?
source
share