23

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() {}
Community
  • 1
  • 1
Tristan Brindle
  • 15,036
  • 2
  • 31
  • 79
  • 14
    The last one is not legal as `auto` deduces the return type as `void`. See http://stackoverflow.com/questions/17134975/will-automatic-return-type-deduction-work-for-main – 0x499602D2 Oct 20 '14 at 17:01
  • @0x499602D2 Doesn't the second quote imply that the return type should be deduced as `int` for case 3? – Tristan Brindle Oct 20 '14 at 17:05
  • @TristanBrindle The answer in the link explains that. – 0x499602D2 Oct 20 '14 at 17:06
  • @0x499602D2 You're right, I was looking at the wrong tab. Looks like all the interesting questions have been asked/answered before... – Tristan Brindle Oct 20 '14 at 17:09
  • @0x499602D2: I'm not convinced that it does, entirely. It's obviously more than a _runtime_ effect otherwise we would expect a compliant compiler to reject the program because there is nothing returned according to the source. If the rules about returning a value from a function are satisfied by `3.6.1/5` then why aren't the rules about type deduction also satisfied by it? – Lightness Races in Orbit Oct 20 '14 at 17:11
  • @Nard 2 and 3 are not legal C++. – James Kanze Oct 20 '14 at 17:13
  • 1
    Out of curiosity, since `int` is the only legal return type why would anyone want to return `auto` for the `main()` function? What would that buy you? – Void Oct 20 '14 at 17:34
  • 1
    @Void Nothing. I was just curious about whether it was technically permitted. – Tristan Brindle Oct 20 '14 at 17:37
  • @TristanBrindle: Ah okay. Cool. :) – Void Oct 20 '14 at 21:29

2 Answers2

18
  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

Jared Burrows
  • 50,718
  • 22
  • 143
  • 180
Marco A.
  • 41,192
  • 25
  • 117
  • 233
  • The return type of a function (other than a lambda) is never deduced. A function declaration must have either the classical form, with the return type in front, or a trailing return type. – James Kanze Oct 20 '14 at 17:36
  • 7
    @JamesKanze That is no longer true for C++14 – Tristan Brindle Oct 20 '14 at 17:40
  • I have to agree with Tristan: n4140 §7.1.6.4/p2: `If the function declarator includes a trailing-return-type (8.3.5), that specifies the declared return type of the function. If the declared return type of the function contains a placeholder type, the return type of the function is deduced from return statements in the body of the function, if any.` – Marco A. Oct 20 '14 at 17:41
  • @MarcoA I'm still not convinced why "the effect of `return 0;`" doesn't include deducing `int` for case (3), but the defect report and the subsequent clarification of the language makes it moot. Thanks very much :-) – Tristan Brindle Oct 20 '14 at 17:45
  • @TristanBrindle that's correct, sorry. I overlooked it. I believe it falls in the same case due to paragraph 5. Anyway I concur that some parts are still kind of "work-in-progress" and there might be ambiguities. – Marco A. Oct 20 '14 at 17:47
  • @TristanBrindle It depends on which paragraph you read (but the intent seems to be to allow yet another useless obfuscation). – James Kanze Oct 20 '14 at 17:52
1

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).

James Kanze
  • 142,482
  • 15
  • 169
  • 310