TL; DR . A call to main results in undefined behavior.
There seems to be a confusion about the terminology used in the standard and the implications for the programmer and compiler.
Firstly, the standard independently defines everything related to the C ++ language. If your particular version of a particular compiler allows for some specific action that has nothing to do with whether this action is legal. For the rest of the post, I mean the ISO03 standard.
So, to quote again, the standard reads in paragraph 3.3.1.3:
The main function should not be used inside the program.
In addition, in §3.2 it is “used” as:
An object or non-overloaded function is used if its name is displayed in a potentially evaluated expression.
This means that as soon as the program starts execution, main should never be entered again . This means that programmers cannot call main , this means that the compiler cannot insert another call to main (why it will, who knows), you cannot take the address of main and call, etc. You may not even have the potential to call main .
The only call to main should be made by the runtime library in which the program is running; all other calls cause undefined behavior. (This means that everything can happen!)
Now about the compiler behavior:
A diagnostic rule is defined as (§1.4.1):
The set of diagnosed rules consists of all syntactic and semantic rules in this International Standard, with the exception of those rules that explicitly indicate that “no diagnostics are required” or that are described as a result of “undefined behavior”.
In our case, in paragraph 3.3.1.3, a diagnostic rule is defined. Here are what compilers should do according to §1.4.2:
- If the program does not contain violations of the rules in this International Standard, the corresponding implementation should, within the limits of its resources, accept and correctly implement 3) this program.
- If the program contains a violation of any diagnosed rule, the corresponding implementation must issue at least one diagnostic message, except that - If the program contains a violation of the rule for which no diagnostics are required, this International Standard does not establish implementation requirements for this program.
Therefore, compilers are not required to apply the rules. All compilers must do is take well-formed programs (§1.3.14) and turn them into an executable program. The compiler is free to warn, mistakenly, etc., however, he likes it if it does not conflict with the language. In accordance with the second sentence, it is necessary to display a message in our particular case.
For this specific gcc task, the -pedantic parameter will warn of the illegality of calling main inside the program. Visual Studio will not warn about calling main , but at any warning level (greater than 0), it will warn about the recursive nature of the program.
What does all this mean in terms of the answers you should expect? This means that it is completely pointless to try to determine with certainty what the code fragment will publish. A call to main results in undefined behavior, and trying to define undefined behavior is obviously a lost cause. The only honest answer anyone can give is “what happens when I call main ?”? "Anything".
Hope this clarifies the situation.