
<html>

<body>



<address>Document Number: P1091R1</address>

<address>Date: 2018-10-08</address>

<address>Reply-to: Nicolas Lesser &lt;blitzrakete@gmail.com&gt;</address>

<address>Audience: EWG</address>



<h1 id="extendingstructuredbindingstobemorelikevariabledeclarations">Extending structured bindings to be more like variable declarations</h1>


<h2 id="changes">Changes</h2>

The following things were changed to R0:

<ul>
  <li>Add lambda captures</li>
  <li>Add structured binding declaration templates</li>
  <li>Add [[maybe_unused]]</li>
  <li>Better wording</li>
</ul>

<h3 id="introduction">Introduction</h3>



<p>There are a lot of restriction on structured bindings compared to variable declarations, like not being able to mark them <code>static</code>, <code>constexpr</code> or them not having unclear linkage. This proposal's aim is to fix this by for example making the underlying structured binding object (and tuple binding variables) have external linkage and by allowing various specifiers (<code>static</code>, <code>thread_local</code>, <code>constexpr</code>, <code>inline</code>, <code>extern</code>) on structured bindings.</p>



<h3 id="motivation">Motivation</h3>



<p>Structured bindings, although very useful, are actually pretty magical. They don't introduce variables in the normal sense for each binding, rather, they are names that refer to specific objects. As such, there are problems including what it means for a binding to be <code>static</code> and how it would work and what linkage do those bindings even have.</p>



<p>Those problems could not be resolved during the discussion of the paper and afterwards, a paper was requested to analyse the possible design impact that such additions to structured bindings would have after two NB comments proposing this were rejected. This paper attempts to do so.</p>



<p>One motivation to do so is to bring structured bindings closer to actual variable declarations, so consistency. This will also make structured bindings more useful, as they are currently lacking for example <code>constexpr</code>, which is becoming every more important for various features of the language.</p>



<p>As a consequence this paper also fixes some DRs that were filed and are under consideration or going to be eventually by either Evolution or Core.</p>



<h3 id="linkage">Linkage</h3>



<p>As per [basic.link]p8, bindings do not have any linkage because they're just names. Currently, in both gcc and clang, tuple binding have linkage, as they were specified as variable declarations in the standard. This is no longer the case though due to the resolution of DR2313<sup>1</sup>.</p>



<p>But the underlying structured binding object is an actual variable, which can have either internal or external linkage depending on the declaration of the structured binding, as gcc and clang do it. There is no way to refer to that object though without making the program IF-NDR as the object has a name like <code>_ZDC1a2bbE</code> for <code>auto[a, bb]</code>.</p>



<p>[dcl.struct.bind]p1 has the following to say about the object:</p>



<blockquote>

  <p>First, a variable with a unique name <em>e</em> is introduced.</p>

</blockquote>



<p>It follows that the variable cannot be referenced in a conforming program anyways, and as such, it doesn't make much sense to give it external linkage. Nonetheless, to be consistent with the rest of the declarations and being able to use just <code>inline</code> (see below) without an extra <code>extern</code> to give the structured binding external linkage, the underlying object should have external linkage.</p>



<h3 id="extern">extern</h3>



<p>As discussed in the previous section, there is no way to reference either the bindings or the underlying object within a conforming program, so allowing <code>extern</code> on such a structured binding would not make much sense.</p>



<p>However, this would prohibit <code>inline</code> on structured bindings that have been declared <code>const</code> and <code>inline</code>, which might be desirable in some cases. For this reason, it should be allowed. Note that <code>extern</code> would have no effect on the individual bindings, except for the tuple case.</p>



<h3 id="staticandthread_local">static and thread_local</h3>



<p><code>static</code> and <code>thread_local</code> on a structured binding make sense and are actually useful. The way to make this work nicely in the standard is to only apply them on the underlying object, and not on the bindings (which wouldn't make sense and can't work today without major changes to the specification of bindings anyways). For the tuple case on the additional variable declarations too.</p>



<p>Because bindings refer to certain objects (depending on the initializer of the structured binding), they would implicitly get the desired semantics of <code>static</code> and <code>thread_local</code>, as they refer to either objects within the underlying object or to separate variables (in the tuple case) which are marked with the desired specifiers.</p>



<h3 id="inline">inline</h3>



<p><code>inline</code> is also useful and will be consistent with inline variables. It will work just as with <code>static</code> and <code>thread_local</code>: The underlying object is marked <code>inline</code> and any additional variables introduced as part of tuple bindings.</p>



<h3 id="constexpr">constexpr</h3>



<p>If <code>constexpr</code> were applied just like <code>static</code> and co. are, then there would be a problem, because the current language rules make the following code ill-formed:</p>



<pre><code class="c++ language-c++">// at block scope

constexpr auto[a] = std::tuple&lt;int&gt;(1);

// "equivalent" to

constexpr auto __sb = std::tuple&lt;int&gt;(1);

constexpr const int&amp; __a = std::get&lt;0&gt;(__sb); // ill-formed today

</code></pre>



<p>A reference must be initialized by a constant expression to be a core constant expression ([expr.const]p2.11], but <code>std::get&lt;0&gt;(__sb)</code> is not a constant expression due to [expr.const]p6.</p>



<p>Richard Smith on the core reflector<sup>2</sup> suggested to relax the restriction on what constitutes a core constant expression of a reference by just requiring that the reference must be initialized by a <em>core</em> constant expression instead (see below to what this change entails).</p>



<p>This would mean that to make structured bindings <code>constexpr</code>, it is necessary to apply <code>constexpr</code> to the underlying object, and apply <code>const</code> to any other variable introduced by tuple bindings.</p>



<p>Of course, one thing to note is that it is important to guarantee that the call to <code>get</code> is a constant expression, because or else <code>constexpr</code> will act like <code>const</code>, which is only maybe a core constant expression.</p>


<h3 id="lambda-captures">lambda captures</h3>

<p>Lambda captures aren't currently allowed to refer to a structured binding. There seems to be no technical reason to disallow this, and indeed, the wording for allowing this just removes the restriction on capturing structured bindings.</p>

<h3 id="maybe_unused">[[maybe_unused]]</h3>

<p>Currently, [[maybe_unused]] cannot be applied to a structured binding declaration. There doesn't seem to be a good reason to disallow this; EDG, clang and gcc all already support [[maybe_unused]] on structured bindings.</p>

<h3 id="template">Template</h3>

<p>The following code is ill-formed as of C++20:</p>

<pre><code class="c++ language-c++">template &lt;auto Var&gt;
constexpr auto[X, Y] = Var;
</code></pre>

<p>As it uses 1) a structured binding declaration template and 2) it is constexpr. The latter is already handled. Should a structured binding be a valid <em>template-declaration</em>? The author argues that yes, it should be. It allows for code that can decompose any non-type template parameter (which now can be any class type, thanks to P0732r2).</p>

<pre><code class="c++ language-c++">constexpr std::pair Position(1, 2);
constexpr std::pair Flag(4, 5);

if (X&lt;Position&gt; == Y&lt;Flag&gt;)
 ; // almost there!
</code></pre>

<p>Note: The author doesn't know how to change the standard to allow this completely.</p>

<h3 id="impact">Impact</h3>

<p>This proposal only makes ill-formed or code with unspecified behavior well-formed in relation to structured bindings.</p>

<p>Due to <code>constexpr</code> tuple binding variables requiring a change to what constitutes a core constant expression, ill-formed code today will become well-formed:</p>

<pre><code class="c++ language-c++">// at block scope

const int var = 1;

const int&amp; ref = var;

static_assert(ref == 1); // ill-formed today, well-formed with this proposal

</code></pre>

<h3 id="proposedwording">Proposed wording</h3>

<p>All changes relative to the latest C++20 draft.</p>

<p>Change [basic.link]p10 (6.5) as follows:</p>

<blockquote>
  <p>Two names that are the same and that are declared in different scopes shall denote the same variable, function, type, template or namespace if</p>
  <ul>
  <li><ins>they denote variables introduced by structured binding declarations
formed with the same sequence of identifiers, or</ins></li>
  </ul>
</blockquote>

<p>Change [expr.prim.lambda.capture]p8 (7.5.5.2) as follows:</p>

<blockquote>
  If a <em>lambda-expression</em> explicitly captures an entity that is not odr-usable<del> or captures a structured binding (explicitly or implicitly)</del>, the program is ill-formed.
</blockquote>

<p>Change [expr.const]p2 (8.6) as follows:</p>

<blockquote>
  <ul>
  <li>an <em>id-expression</em> that refers to a variable or data member of reference type unless the reference has a preceding initialization and either
  
  <ul>
  <li>it is initialized with a <ins>core</ins> constant expression or</li>
  
  <li>its lifetime began within the evaluation of e;</li></ul>
  </li>
  </ul>
</blockquote>

<p>Change [dcl.attr.unused]p2 (9.11.8) as follows:</p>

<blockquote>
  <p>The attribute may be applied to the declaration of a class, a <em>typedef-name</em>, a variable, <ins>a structured binding declaration,</ins> a non-static data member, a function, an enumeration, or an enumerator.</p>
</blockquote>

<p>Change [dcl.dcl]p8 (10) as follows:</p>

<blockquote>
  <p>A <em>simple-declaration</em> with an <em>identifier-list</em> is called a <em>structured binding declaration</em> ([dcl.struct.bind]). The <em>decl-specifier-seq</em> shall contain only <ins>constexpr, inline,</ins> the <em>type-specifier</em> auto<ins>, the <em>storage-class-specifier</em>s static and thread_local,</ins> and <em>cv-qualifiers</em>. <ins>The <em>decl-specifier-seq</em> shall only contain the <em>storage-class-specifier</em> extern if the <em>simple-declaration</em> is not a <em>declaration-statement</em>.</ins></p>
</blockquote>

<p>Change [dcl.stc]p3 (10.1.1) as follows:</p>

<blockquote>
  <p>The <em>thread_local</em> specifier indicates that the named entity has thread storage duration. It shall be applied only to the names of variables of namespace or block scope<ins>, to structured binding declarations ([dcl.struct.bind]),</ins> and to the names of static data members.</p>
</blockquote>

<p>Change [dcl.stc]p4 (10.1.1) as follows:</p>

<blockquote>
  <p>The <em>static</em> specifier can be applied only to names of variables and functions<ins>, to structured binding declarations ([dcl.struct.bind]),</ins> and to anonymous unions.</p>
</blockquote>

<p>Change [dcl.stc]p5 (10.1.1) as follows:</p>

<blockquote>
  <p>The <em>extern</em> specifier can be applied only to the names of variables and functions <ins> and to structured binding declarations ([dcl.struct.bind])</ins>.</p>
</blockquote>

<p>Change [dcl.constexpr]p1 (10.1.5) as follows:</p>

<blockquote>
  <p>The <em>constexpr</em> specifier shall be applied only to the definition of a variable or variable template or the declaration of a function or function template<ins>, or to a structured binding declaration ([dcl.struct.bind])</ins>.</p>
</blockquote>

<p>Change [dcl.inline]p1 (10.1.6) as follows:</p>

<blockquote>
  <p>The <em>inline</em> specifier can be applied only to the declaration or definition of a variable or function<ins>, or to a structured binding declaration ([dcl.struct.bind])</ins>.</p>
</blockquote>

<p>Change [class.copy.elision]p1 (10.9.5) as follows:</p>

<blockquote>
  <p>This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies):</p>
  <ul>
    <li><del>in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function parameter or a variable introduced by the exception-declaration of a handler ([except.handle])) with the same type (ignoring cv-qualification) as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function call's return object</del></li>
    <li><del>in a throw-expression, when the operand is the name of a non-volatile automatic object (other than a function or catch-clause parameter) whose scope does not extend beyond the end of the innermost enclosing try-block (if there is one), the copy/move operation from the operand to the exception object ([except.throw]) can be omitted by constructing the automatic object directly into the exception object</del></li>
    <li>when the exception-declaration of an exception handler ([except]) declares an object of the same type (except for cv-qualification) as the exception object ([except.throw]), the copy operation can be omitted by treating the exception-declaration as an alias for the exception object if the meaning of the program will be unchanged except for the execution of constructors and destructors for the object declared by the exception-declaration. [ Note: There cannot be a move from the exception object because it is always an lvalue. - end note ]</li>
    <li><ins>for a structured binding. The rest of this paragraph is followed with the object that the structured binding refers to.</ins></li>
    <li><ins>for an expression e who names a non-volatile automatic object (other than a function or catch-clause parameter)</ins></li>
    <ul><li><ins>in a return statement with a class return type when e has the same type (ignoring cv-qualification) as the return type, the copy/move operation can be omitted by constructing the automatic object directly into the function call's return object</ins></li></ul>
    <ul><li><ins>in a <em>throw-expression</em> when the object (designated by e)'s scope does not extend beyond the end of the innermost enclosing <em>try-block</em> (if there is one), the copy/move operation from the operand to the exception object ([expr.throw]) can be omitted by constructing the automatic object directly into the exception object</ins></li></ul>
  </ul>
</blockquote>

<p>Change [dcl.struct.bind]p1 (11.5) as follows:</p>

<blockquote>
  <p>A structured binding declaration introduces the <em>identifiers</em> v<sub>0</sub>, v<sub>1</sub>, v<sub>2</sub>, ... of the <em>identifier-list</em> as names ([basic.scope.declarative]) of structured bindings. Let <em>cv</em> denote the <em>cv-qualifiers</em> in the <em>decl-specifier-seq</em>. First, a variable with a <del>unique</del> name <em>e</em> is introduced<ins>, where <em>e</em> is unique except as specified in Clause 6</ins>. If the <em>assignment-expression</em> in the <em>initializer</em> has array type A and no <em>ref-qualifier</em> is present, <em>e</em> <del>has type <em>cv</em> A</del><ins> is declared as-if by:</ins><p>

<p><ins><em>attribute-specifier-seq<sub>opt</sup></em> <em>decl-specifier-seq'</em> A e;</ins></p>

<p><ins>where <em>decl-specifier-seq'</em> is the <em>decl-specifier-seq</em> without the <em>type-specifier</em> auto</ins>, and each element is copy-initialized or direct-initialized from the corresponding element of the <em>assignment-expression</em> as specified by the form of the <em>initializer</em>.</p>
</blockquote>

<p>Change [dcl.struct.bind]p3 (11.5) as follows:</p>

<blockquote>
  <p>Otherwise, if the <em>qualified-id</em> <code>std::tuple_size&lt;E&gt;::value</code> names a complete type, the expression <code>std::tuple_size&lt;E&gt;::value</code> shall be a well-formed integral constant expression and the number of elements in the <em>identifier-list</em> shall be equal to the value of that expression. The <em>unqualified-id</em> get is looked up in the scope of <em>E</em> by class member access lookup ([basic.lookup.classref]), and if that finds at least one declaration that is a function template whose first template parameter is a non-type parameter, the initializer is <code>e.get&lt;i&gt;()</code>. Otherwise, the initializer is <code>get&lt;i&gt;(e)</code>, where <em>get</em> is looked up in the associated namespaces. In either case, <code>get&lt;i&gt;</code> is interpreted as a <em>template-id</em>. [ Note: Ordinary unqualified lookup is not performed.  end note ] In either case, <em>e</em> is an lvalue if the type of the entity <em>e</em> is an lvalue reference and an xvalue otherwise. Given the type T<sub>i</sub> designated by <code>std::tuple_element&lt;i, E&gt;::type</code>, variables are introduced with unique names <em>r<sub>i</sub></em> <ins>as if by:</ins><del>of type "reference to T<sub>i</sub>" initialized with the initializer ([dcl.init.ref]), where the reference is an lvalue reference if the initializer is an lvalue and an rvalue reference otherwise.</del></p>

<p><ins><em>decl-specifier-seq'</em> <em>cv</em> T<sub>i</sub>&amp; r<sub>i</sub> = /*initializer*/;</ins></p>

<p><ins>if the initializer is an lvalue and by:</ins></p>

<p><ins><em>decl-specifier-seq'</em> <em>cv</em> T<sub>i</sub>&amp;&amp; r<sub>i</sub> = /*initializer*/;</ins></p>

<p><ins>otherwise. <em>decl-specifier-seq'</em> refers to the <em>decl-specifier-seq</em> of the structured binding declaration except for the <em>type-specifier</em> auto and constexpr. <em>cv</em> is const if constexpr is in the <em>decl-specifier-seq</em>, otherwise it is empty.</ins></p>

</blockquote>



<h3 id="acknowledgements">Acknowledgements</h3>



<p>Thanks to k-ballo, T.C, Richard Smith, Alberto Barbati for mentioning and solving specific problems with this proposal, Jens Maurer for the help in writing the wording, and the countless others on the #future-standard Cpplang channel and the std-proposals mailing list.</p>



</body>

</html>


