1. Revision History
1.1. Revision 0
Initial Release 🎉
2. Motivation
Main is a sweet special little gremlin in C++. It has interesting properties
regarding exceptions and noreturn functions,
is actually deprecated on
some unicode aware platforms and it even lets us ignore the return type
entirely. And yet, this is the function we introduce to beginners from day one.
This magic, bizarre, entry point to our programs is a nightmare. This proposal
attempts to solve this, while also standardizing existing extensions and archaic literature and examples.
This paper currently works in tandem with the Desert Sessions proposal, which attempts to solve issues regarding
and the lack of
modern support for reading from environment variables. However, it is not a
prerequisite of adding the [p1275] API into the standard, nor does [p1275] require that this paper be added to the standard.
3. Design
The idea behind
is to simply state that a
that takes no
arguments can choose to return nothing as well. Given that we currently permit
this with the
returning main signatures this will not break existing
code.
4. Wording
All wording is relative to [N4762]
In Section 6.8.3.1
function [basic.start.main] paragraph 2, insert
2An implementation shall not predefine thefunction. This function shall not be overloaded. Its type shall have C++ language linkage and it have a declared return type of either type
main or type
int , but otherwise its type is implementation-defined. An implementation shall allow:
void
- (2.1)— a function of
returning
() and
void (2.1)(2.2) — a function ofreturning
() and
int (2.2)(2.3) — a function of, pointer to pointer to
( int returning
char )
int
In paragraph 5, insert:
5Astatement in
return has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling
main with the return value or
std :: exit in the case of no return value as the argument. If control flows off the end of the compound-statement of
0 , the effect is equivalent to a
main with operand
return (see also 13.3) , or calling
0 once
std :: exit ( 0 ) 's scope has exited
main