Search code examples
c++program-entry-pointlanguage-lawyerc++14

Legal definitions of main() in C++14


The last draft of C++14 that I was able to find says, regarding main() [3.6.1]:

An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both

— a function of () returning int and

— a function of (int, pointer to pointer to char) returning int

and (paragraph 5)

If control reaches the end of main without encountering a return statement, the effect is that of executing

return 0;

Does this mean that all of the following are legal C++14 minimal programs? If any isn't, why not?

  1. auto main() -> int {}
  2. auto main() { return 0; }
  3. auto main() {}

Solution

    1. Is legal, the second and the latter aren't because of the following reasons:

    2. The return type of the main function cannot be deduced since CWG 1669 was accepted and the standard will be reworded as:

      An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a declared return type of type int, but otherwise its type is implementation-defined.

      This got its way into n4140. More on this: http://wg21.cmeerw.net/cwg/issue1669

    3. The same as above