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?

  • auto main() -> int {}
  • auto main() { return 0; }
  • auto main() {}

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

  • 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

  • The same as above


  • The first is probably legal. The other two certainly not (§7.1.6.4/1): "The auto type-specifier signifies that the type of a variable being declared shall be deduced from its initializer or that a function declarator shall include a trailing-return-type."

    I say probably for the first, because §3.6.1 isn't really clear to what degree the definition must match. A 100% textual match isn't required, even if that is what a literal interpretation of what it says would imply: in the text, the body of main is given as /* ... */ , which isn't very useful, and certainly isn't required. Similarly, tradition has also allowed any name for argc and argv , and declaring argv as char** argv . There's no tradition about defining main with a trailing return type, however. I think that what was meant in §3.6.1 is that all definitions of main which have the same signature would be allowed, but this is not what the actual words say.

    Regardless: why would one want such obfuscation? C/C++ is not Pascal, and what is natural in one language (like trailing return types for functions) is obfuscation in another.

    EDIT:

    I've just downloaded a more recent draft (N3797), and it seems like the wording has changed (and is now contradictory). The first paragraph still says basically the same thing (but includes addtiional wording for for lambdas): the auto is replaced "either by deduction from an initializer or by explicit specification with a trailing-return-type." (And of course, fucntions don't have initializers, so only the trailing-return-type can apply.) Later paragraphs, however, do speak about deduction from the return type, even though the first paragraph requires the trailing return type (and thus makes deduction superfluous).

    链接地址: http://www.djcxy.com/p/28738.html

    上一篇: ExitCodes大于255,可能吗?

    下一篇: main()在C ++中的法律定义14