19

C++03 3.6.1.3: The function main shall not be used (3.2) within a program. ...

I wonder why this rule exists... Is anyone aware of any system/implementation where it would be a problem if main were used?

P.S. 1. I know the definition of the term used. 2. I know there are simple workarounds like calling a single MyMain() from main() and using MyMain() instead. 3. The question is about real-world implementations which would have a problem if the restriction weren't there. Thanks!

5
  • IIRC, there is no such restriction in C, so I'd say the answer is "no".
    – avakar
    Commented Nov 10, 2010 at 11:45
  • I think it means, main will not be called by any piece of code in your program (else it would just load a top itself).
    – leppie
    Commented Nov 10, 2010 at 11:46
  • Possible duplicate of Can main function call itself in C++?
    – phuclv
    Commented Aug 26, 2016 at 15:43
  • Late to the party, but if within 10 years nobody has come up with an implementation where explicitly calling main() would be a problem I guess the answer is "no": Commented Dec 19, 2020 at 20:01
  • Related: Why does gcc warn about decltype(main()) but not clang? goes into detail about what exactly it means to "use" main. Commented Sep 12, 2023 at 15:04

3 Answers 3

38

In addition to the other answers: The c++ spec guarantees that all static initialization has happened before main is called.

If code could call main then some static scoped object could call main, in which case a fundamental guarantee is violated.

The spec can't say "static scoped objects should not call main()" because many objects are not written specifically to be instantiated at static scope always. It also can't say that constructors should not call main() - because its very difficult to audit and prove that a constructor isn't calling a method, calling a method, that might sometimes, call main().

1
  • 2
    The standard could easily say that it's undefined behavior to call the main function during dynamic initialization. It could also easily say that it's UB to call the main function before the main function has been called automatically by the implementation. Commented Oct 1, 2023 at 10:43
19

I'd imagine this preserves an implementation's freedom to prefix main with code to construct globals and statics, accept any parameters identifying the environment and command line arguments and map them to the argc/argv/env conventions of C++, construct an appropriate stack and exception framework for the application to execute etc. Consider that not all environments may allow an executable image to have any other symbol designated as initialisation code to be run before main().

Similarly, cleanup code may be appended to main(), along with a call to the OS with some mapping from the 0/non-zero convention of C and C++ to the actual success/failure values used by that specific OS.

Consequently, calling main from elsewhere could attempt a second reinitialisation of the application framework or force an unintended exit to the OS - sounds catastrophic to me.

7

C++'s main() is a strange little function that has different syntax for exception-handling, doesn't have to return a value, even though it has to be defined as returning int, etc. I don't know whether this affects any real implementations, but I would guess that the restriction exists to give compiler writers some latitude in how they implement main().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.