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 typemain or typeint , but otherwise its type is implementation-defined. An implementation shall allow:void
- (2.1)— a function of
returning() andvoid (2.1)(2.2) — a function ofreturning() andint (2.2)(2.3) — a function of, pointer to pointer to( int returningchar ) int
In paragraph 5, insert:
5Astatement inreturn has the effect of leaving the main function (destroying any objects with automatic storage duration) and callingmain with the return value orstd :: exit in the case of no return value as the argument. If control flows off the end of the compound-statement of0 , the effect is equivalent to amain with operandreturn (see also 13.3) , or calling0 oncestd :: exit ( 0 ) 's scope has exitedmain