<html>
<head>

<title>N4357: [[noexit]] attribute for main</title>

<style type="text/css">
  ins { text-decoration:underline; font-weight:bold; background-color:#A0FFA0 }
  .new { text-decoration:none; font-weight:bold; background-color:#D0FFD0 }
  del { text-decoration:line-through; background-color:#FFA0A0 }
  strong { font-weight: inherit; color: #2020ff }
</style>

</head>

<body>
N4357<br/>
revision of N4226<br/>
Jens Maurer<br/>
2015-01-19

<h1>N4357: [[noexit]] attribute for <code>main</code></h1>

<h2>Introduction</h2>

This paper proposes to add a <code>[[noexit]]</code> attribute, only
applicable to the function <code>main</code>, as a hint to eliminate
destructor calls for objects with static storage duration.  More
detailed rationale and discussion is available in the predecessor
paper N4226.

<p>
This paper was discussed during the November 2014 WG21 meeting in
Urbana-Champaign.  It was initially approved by the Evolution Working
Group, but rejected by the Core Working Group.  The latter opined that
in order to benefit from the proposal, undesirable linker magic would
be required.  A way of specifying global options, attributes, or
#pragmas visible in every translation unit would be preferred.  After
that, Evolution Working Group discussed this proposal again, noting
that it supported the general idea, but required a more comprehensive
paper analyzing the effects on different translation units, the
effects on shared libraries, and the effects (such as undefined
behavior) if violated.  Also, measurements on the resource savings
(such as ROM and RAM size) would be much appreciated.
</p>

<p>
This paper does not attempt to address the WG21 concerns raised in the
discussion, it merely documents the state of the discussion to
facilitate future work.
</p>


<h2>Wording</h2>


Add a new paragraph after 3.6.1 basic.start.main paragraph 5:

<blockquote class="new">
[ Note: The function <code>main</code> may be defined with the
<code>noexit</code> attribute; see 7.6.6 dcl.attr.noexit.  -- end note
]

</blockquote>


Add a new section 7.6.6 dcl.attr.noexit:

<blockquote class="new">
<b>7.6.6 [dcl.attr.noexit] Noexit attribute</b><p/>

The <em>attribute-token</em> <code>noexit</code> specifies that a
program never exits in a normal fashion. It shall appear at most once
in each <em>attribute-list</em> and no
<em>attribute-argument-clause</em> shall be present. The attribute may
be applied to the <em>declarator-id</em> of the definition of the
function <code>main</code> (3.6.1 basic.start.main).

<p>

If the function <code>main</code> is defined with the
<code>noexit</code> attribute, leaving the function <code>main</code>
(3.6.1 basic.start.main) results in undefined behavior and an odr-use
(3.2 basic.def.odr) of the function <code>std::exit</code> in any
translation unit is ill-formed; no diagnostic required. [ Note: The
function <code>main</code> may exit via an exception, in which case
<code>std::terminate</code> is called (15.5.1
except.terminate). Destructors for objects with static storage
duration are never invoked if <code>main</code> is defined with the
<code>noexit</code> attribute (3.6.3 basic.start.term). -- end note ]

</blockquote>


Change in 18.5 paragraph 6:

<blockquote>

Implementation limits: The implementation shall support the
registration of at least 32 functions. <ins>[ Note: If the
function <code>main</code> is defined with the <code>noexit</code>
attribute (7.6.6 dcl.attr.noexit), calling the <code>atexit</code>
function has no effect, since the functions so registered are never
invoked. -- end note ]</ins>

</blockquote>


</body>
</html>
