<html>
<head>
<meta charset="UTF-8">
<title>P2360R0: Extend init-statement to allow alias-declaration </title>

<style type="text/css">
  ins { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
  .new { text-decoration:none; background-color:#D0FFD0 }
  del { text-decoration:line-through; background-color:#FFA0A0 }  
  strong { font-weight: inherit; color: #2020ff }
  table, td, th { border: 1px solid black; border-collapse:collapse; padding: 5px }
</style>
</head>

<body>
ISO/IEC JTC1 SC22 WG21 P2360R0<br/>
Author: Jens Maurer<br/>
Target audience: EWG<br/>
2021-04-13<br/>

<h1>P2360R0: Extend <em>init-statement</em> to allow <em>alias-declaration</em></h1>

<h2>Introduction</h2>

C++98 and onwards allowed to declare a local variable in a <code>for</code> loop:

<pre>
  for (int i = 0; i < 10; ++i)
    /* something */;
</pre>

The papers
 <ul>
    <li>P0305R1 Selection statements with initializer and</li>
    <li>P0614R1 Range-based for statements with initializer
</ul>
extended this facility such that additional variables can be declared
in <code>if</code>, <code>switch</code>, and range-based
<code>for</code> statements.
<p>
Grammatically, this is an <em>init-statement</em>, which allows
expressions and variable declarations as well as typedefs, but
(inconsistently) not <em>alias-declaration</em>s.  This paper proposes
to also allow <em>alias-declaration</em>s.
<p>
  
<table>
  <tr><th>C++20</th><th>this paper</th></tr>
  <tr>
    <td>
<pre>
  for (typedef int T; T e : v)
    /* something */;
</pre>
    </td>
    <td>
<pre>
  for (using T = int; T e : v)
    /* something */;
</pre>
  </tr>
</table>


<h2>Rationale</h2>

This is an obvious inconsistency.  For modern code, it is generally
recommended to use <em>alias-declaration</em>s in lieu of typedefs
(e.g. <a href="https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rt-using">https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rt-using</a>).
The example shown in the introduction is the only case where this fails.
<p>
The alternative would be to prohibit typedefs as
an <em>init-statement</em>, but while unusual, they seem useful in
some situations.  After all, the argument of a reduced scope for
variables introduced in an <em>init-statement</em> applies to typedefs
as well.

<h2>Miscellanea</h2>

No feature test macro is proposed.  If a user cares about backward
compatibility, a typedef should be written unconditionally.  The
argument for a <code>#error</code> check seems weak, since the compiler error
message for an unrecognized <em>alias-declaration</em> is unconditional
and should be clear.
<p>
This has not been implemented.

<h2>Wording</h2>

Change in 8.1 [stmt.pre] paragraph 1:

<blockquote>
<pre>
<em>init-statement:
    expression-statement
    simple-declaration
    <ins>alias-declaration</ins></em>
</pre>
</blockquote>
