0
int x = 3*x; // x = 0
int y = y+1; // y = 1

Do the C++ specs mention that any variable definition involving a self-reference will have its recursive reference set to 0 for the scope of the assignment / initialization? I was genuinely surprised this even compiles.

Niklas Vest
  • 872
  • 6
  • 20
  • 1
    Int declarations clear out to 0 on Linux or at least on compilers I've seen on Linux. They're set to garbage (random int) on Windows or at least on compilers I've seen on Windows. – Spidey Apr 23 '22 at 09:17
  • @Spidey thanks for the swift answer; i guess that means it compiles but has undefined (i.e. its up to the compiler implementation) behavior? – Niklas Vest Apr 23 '22 at 09:19
  • @NiklasVest Yes, it is undefined behaviour. Which means any outcome is possible (including initialisation to zero, a garbage value, or even [relatively rare in practice] reformatting your hard drive). One property of undefined behaviour is also "no diagnostic required", mostly because there are some (more complicated) examples in which detecting such things is quite expensive (even prohibitive) for compilers. – Peter Apr 23 '22 at 09:39

0 Answers0