<html>
<style type="text/css">
  ins { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
  del { text-decoration:line-through; background-color:#FFA0A0 }
</style>
<title>static_assert and list-initialization in constexpr functions</title>
<body>
Jason Merrill
<br>2011-03-25
<br>Revision 10
<br>N3268=11-0038

<h2><b><tt>static_assert</tt> and list-initialization in <tt>constexpr</tt> functions</b></h2>

<h3>Introduction</h3>

This paper provides drafting to resolve core issues 837 and 898.

<H4>898.
  
Declarations in constexpr functions
</H4><B>Section: </B>7.1.5&#160; [dcl.constexpr]
 &#160;&#160;&#160;



<P>According to 7.1.5 [dcl.constexpr] paragraph 3, no declarations
are permitted in the body of a constexpr function.  This seems overly
restrictive.  At least three kinds of declarations would seem to be
helpful in writing such functions: <TT>static_assert</TT>,
<TT>typedef</TT> and alias declarations, and local automatic variables
initialized with constant expressions.  (Similar considerations apply
to lambdas in which the <I>lambda-return-type-clause</I> is omitted.)</P>

<H4>837.
  
Constexpr functions and <TT>return</TT> <I>braced-init-list</I>
</H4><B>Section: </B>7.1.5&#160; [dcl.constexpr]
 &#160;&#160;&#160;



<P>The body of a constexpr function is required by
7.1.5 [dcl.constexpr] paragraph 3 to be of the form</P>

<UL><TT>{ return</TT> <I>expression</I><TT>; }</TT></UL>

<P>However, there does not seem to be any good reason for prohibiting
the alternate return syntax involving a <I>braced-init-list</I>.
The restriction should be removed.</P>

<h3>Proposed Resolution</h3>

Change 6.6.3 [stmt.return] paragraph 2 as follows:</P></LI>

<BLOCKQUOTE>

A return statement <SPAN style="text-decoration:line-through;background-color:#FFA0A0">without an expression</SPAN> <SPAN style="font-weight:bold;background-color:#A0FFA0">with neither an
<I>expression</I> nor a <I>braced-init-list</I></SPAN> can be used only
in functions that do not return a value...

</BLOCKQUOTE>

7.1.5/3:

<blockquote>
<li>its function-body shall be a compound-statement <del>of the form</del>
<pre>
  <del>{ return <i>expression</i> ; }</del>
</pre>
<ins>that contains only</ins>
<ul>
  <li><ins>null statements,</ins></li>
  <li><ins><i>static_assert-declaration</i>s,</ins></li>
  <li><ins><tt>typedef</tt> declarations and <i>alias-declaration</i>s that do not define classes or enumerations,</ins></li>
  <li><ins><i>using-declaration</i>s,</ins></li>
  <li><ins><i>using-directive</i>s,</ins></li>
  <li><ins>and exactly one return statement.</ins></li>
</ul></li>
<LI>every <SPAN style="font-weight:bold;background-color:#A0FFA0">constructor call and</SPAN> implicit conversion
used in <SPAN style="text-decoration:line-through;background-color:#FFA0A0">converting <I>expression</I> to the function return
type</SPAN> <SPAN style="font-weight:bold;background-color:#A0FFA0">initializing the return value</SPAN>
(<SPAN style="font-weight:bold;background-color:#A0FFA0">6.6.3 [stmt.return],</SPAN> 8.5 [dcl.init])
shall be one of those allowed in a constant expression
(5.19 [expr.const]).</P></LI>
</blockquote>

7.1.5/4:

<blockquote>
  <li>the compound-statement of its function-body shall <del>be
      empty</del><ins>contain only</ins>
<ul>
  <li><ins>null statements,</ins></li>
  <li><ins><i>static_assert-declaration</i>s,</ins></li>
  <li><ins><tt>typedef</tt> declarations and <i>alias-declaration</i>s that do not define classes or enumerations,</ins></li>
  <li><ins><i>using-declaration</i>s,</ins></li>
  <li><ins>and <i>using-directive</i>s.</ins></li>
</ul></li>
</blockquote>

7.1.5/5:

<blockquote>
Function invocation substitution for a call of a constexpr function or of a
constexpr constructor means implicitly converting each argument <del>expression</del>
to the corresponding parameter type as if by copy-initialization [
Footnote: ... --end footnote], substituting that converted expression for
each use of the corresponding parameter in the function-body, and, for
constexpr functions, implicitly converting the resulting <ins>returned</ins>
expression <ins>or <i>braced-init-list</i></ins> to the return type of the
function <ins>as if by copy-initialization</ins>. Such substitution does
not change the meaning. [ Example: ... --end example]
<p>For a constexpr function, if no function argument values
exist such that the function invocation substitution would produce a
constant expression (5.19 expr.const), the program is ill-formed; no
diagnostic required. For a constexpr constructor, if no argument values
exist such that after function invocation substitution, every constructor
call and full-expression in the mem-initializers would be a constant
expression (including conversions), the program is ill-formed; no
diagnostic required.
</blockquote>

</body>
</html>
