I was playing with my C++ compiler and I noticed a recursive dependency of a global variable and a function call is not rejected. Example:
#include <iostream>
int foo();
int t = foo();
int foo()
{
std::cout << "Hello before main: " << t << "\n";
t = 10;
return t + 10;
}
int main()
{
std::cout << "Hello from main.\n";
}
This compiled program prints out the following:
Hello before main: 0
Hello from main.
So when I use declare the t
it has a dependency on the foo
function which again has a dependency on t
. The C++ compiler breaks this loop by essentially having t
be zero initialized before the assignment expression runs.
This was surprising to me. Is this correct behavior according to the standard or am I invoking some undefined behavior here?