Legal definitions of main() in C++14

二次信任 提交于 2019-11-29 01:12:12
Marco A.
  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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!