Void Main

Published Proposal,

This version:
Isabella Muerte
ISO/IEC JTC1/SC22/WG21 14882: Programming Language — C++
Current Render:
Current Source:


The current entrypoint for C++ is bizarre, special cased, and violates our own best practices. Let’s make void a valid return type for main.

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, argv 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 argv 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 void main() is to simply state that a main() that takes no arguments can choose to return nothing as well. Given that we currently permit this with the int returning main signatures this will not break existing code.

4. Wording

All wording is relative to [N4762]

In Section main function [basic.start.main] paragraph 2, insert

2An implementation shall not predefine the main function. This function shall not be overloaded. Its type shall have C++ language linkage and it have a declared return type of either type int or type void , but otherwise its type is implementation-defined. An implementation shall allow:
  1. (2.1)— a function of () returning void and
  2. (2.1) (2.2) — a function of () returning int and
  3. (2.2) (2.3) — a function of (int, pointer to pointer to char) returning int

In paragraph 5, insert:

5A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling std::exit with the return value or 0 in the case of no return value as the argument. If control flows off the end of the compound-statement of main, the effect is equivalent to a return with operand 0 (see also 13.3) , or calling std::exit(0) once main's scope has exited


Informative References

Richard Smith. Working Draft, Standard for Programming Language C+. 7 July 2018. URL: https://wg21.link/n4762
Isabella Muerte. Desert Sessions: Improving Hostile Environment Interactions. URL: https://wg21.link/p1275r0