Possible Duplicate:
Is it legal to recurse into main() in C++?
#include <iostream>
using namespace std;
int main() {
static int var = 5;
std::cout << --var;
if(var)
main();
}
gcc compiles the code http://ideone.com/lIp3A . I know that main cannot be used inside main in C++. How come this code compiles?
The code is ill-formed because it violates the
shall
construct of§3.6.1.3
§3.6.1.3 says :
The shall construct
A diagnosable rule is defined as (§1.4.1):
§3.6.1.3
defines a diagnosable rule.According to §1.4.2:
Conclusion
A compiler is free to do whatever it wants to do. Try the same code on Comeau Online (a more conforming compiler). I get this error
"function "main" may not be called or have its address taken"