Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is calling the main function supposedly undefined behavior (UB)

I fear this is again a question about interpreting the ISO/IEC 14882 (the C++ standard) but:

Is calling main from the program e.g. my calling main() recursively from main not at least implementation defined behavior? (Update: I imply later ill-formed not implementation defined, also not UB, see below and answer)

6.9.3.1 [basic.start.main] states

3 The function main shall not be used within a program. The linkage (6.6) of main is implementation-defined...

The consensus seems to be undefined behavior (UB). The documentation of MSVC also points towards UB, the one of gcc also implicitly denies implementation-defined behavior. It can not be [defns.unspecified] unspecified behavior since I would interpret shall not as ill-formed.

However, despite the implementations, to my interpretation is should not be UB but as 4.1 [intro.compliance] states

1 The set of diagnosable rules consists of all syntactic and semantic rules in this document except for those rules containing an explicit notation that “no diagnostic is required” or which are described as resulting in “undefined behavior”. ... (2.2) — If a program contains a violation of any diagnosable rule or an occurrence of a construct described in this document as “conditionally-supported” when the implementation does not support that construct, a conforming implementation shall issue at least one diagnostic message.

For me the reasoning seems clear


tl;dr

  1. calling main implies the program contains a violation of the rule of [basic.start.main]
  2. [basic.start.main] does not state calling/use is UB or a diagnostic is not required
    1. is an element of "diagnosable rules" as per [intro.compliance]
  3. [intro.compliance] 2.2 states violation of any diagnoseable rule must be issued at least one diagnostic message
  4. Since 3. and 4. the usage of main shall issue at least one diagnostic message
  5. Since 5. 1. is not UB
  6. Since neither gcc,MSVC or clang issue an error or warning but compile, all major implementations are not compliant

Of course since 7. I feel again in the Don Quixote scenario i.e. being wrong, so I would appreciate if someone could enlighten me about my mistake. Otherwise, there are standard defects, aren't there?

like image 811
Superlokkus Avatar asked Apr 07 '26 20:04

Superlokkus


1 Answers

I think your analysis is correct: calls to main are ill-formed.

You have to pass the -pedantic flag to make GCC and Clang conform. In that case, Clang says

warning: ISO C++ does not allow 'main' to be used by a program [-Wmain]

and GCC says

warning: ISO C++ forbids taking address of function '::main' [-Wpedantic]

But they allow calls to main as an extension. The standard permits such an extension, since it doesn't change the meaning of any conforming programs.

like image 53
Brian Bi Avatar answered Apr 09 '26 12:04

Brian Bi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!