<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
  <head>
    <meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
    <title>Allow lambda capture [=, this]</title>
    <style type="text/css">
      html { margin: 0; padding: 0; color: black; background-color: white; }
      body { padding: 2em; font-size: medium; font-family: "DejaVu Serif", serif; line-height: 150%; }
      code { font-family: "DejaVu Sans Mono", monospace; color: #006; }

      h1, h2, h3 { margin: 1.5em 0 .75em 0; line-height: 125%; }

      div.code { white-space: pre-line; font-family: "DejaVu Sans Mono", monospace;
                 border: thin solid #E0E0E0; background-color: #F8F8F8; padding: 1em;
                 border-radius: 4px; }

      div.strictpre { white-space: pre; }

      sup, sub { line-height: 0; }

      .docinfo { float: right; }
      .docinfo p { margin: 0; text-align:right; }
      .docinfo address { font-style: normal; }

      .quote { display: inline-block; clear: both; margin-left: 1ex;
                 border: thin solid #E0E0E0; background-color: #F8F8F8; padding: 1ex; }

      .modify { border-left: thick solid #999; border-right: thick solid #999; padding: 0 1em; }
      .insert { border-left: thick solid #0A0; border-right: thick solid #0A0; padding: 0 1em; }
      .comment { color: #456; }

      p.skip { margin-top: 1em; }

      ins { color: #090; }
      del { color: #A00; }
      ins code, del code { color: inherit; }
    </style>
  </head>
  <body>
    <div class="docinfo">
      <p>Date: 2016-06-27</p>
      <address>Thomas K&ouml;ppe &lt;<a href="mailto:tkoeppe@google.com">tkoeppe@google.com</a>&gt;</address>
      <p class="skip">ISO/IEC JTC1 SC22 WG21 P0409R0</p>
      <p>To: EWG</p>
    </div>

    <h1>Allow lambda capture <code>[=, this]</code></h1>

    <!-- fgrep -e "<h2 id=" allow_this.html | sed -e 's/.*id="\(.*\)">\(.*\)<\/h2>/<li><a href="#\1">\2<\/a><\/li>/g' -->
    <h2>Contents</h2>
    <ol>
      <li><a href="#history">Revision history</a></li>
      <li><a href="#proposal">Proposal</a></li>
      <li><a href="#impact">Impact on the Standard</a></li>
      <li><a href="#wording">Formal wording</a></li>
    </ol>

    <h2 id="history">Revision history</h2>
    <ul>
      <li>P0409R0: Initial proposal.</li>
    </ul>

    <h2 id="proposal">Proposal</h2>

    <p>We propose to allow <code>[=, this]</code> as a lambda capture.</p>

    <p>With the introduction of capture-<code>this</code>-by-value in C++17, it may be
      desirable to increase code readability by being explicit about the kind of capture
      in the presence of a <code>[=]</code>-default:</p>

    <div class="code">std::function&lt;void()&gt; X::f(int n) {
      &nbsp; return coin() ? [=,&nbsp; this]() { g(n, x_, y_, z_); }
      &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : [=, *this]() { h(n, x_, y_, z_); };
      }</div>

    <p>By contrast, when both <code>[=]</code> and <code>[=, *this]</code> are present
      in a code base in large numbers, it may be easy to forget that the former is different
      from the latter: It is hard to notice the absence of something. In such situations, authors
      may wish to be explicit and to spare the reader from having remember the correct default.</p>

    <h2 id="impact">Impact on the Standard</h2>

    <p>The proposal is a pure core language extension. The newly allowed syntax was
      previously ill-formed.</p>

    <h2 id="wording">Formal wording</h2>

    <p>In section 5.1.5 [expr.prim.lambda], change paragraph 9 as follows.</p>

    <div class="modify">
      <p>If a <em>lambda-capture</em> includes a <em>capture-default</em> that is <code>&amp;</code>,
        no identifier in a <em>simple-capture</em> of that <em>lambda-capture</em> shall be preceded
        by <code>&amp;</code>. If a <em>lambda-capture</em> includes a <em>capture-default</em> that
        is <code>=</code>, each <em>simple-capture</em> of that <em>lambda-capture</em> shall be of
        the form &ldquo;<code>&amp;</code> <em>identifier</em>&rdquo;<ins>, &ldquo;<code>this</code>&rdquo;</ins>
        or &ldquo;<code>* this</code>&rdquo;. [<em>Note</em>: The form <code>[&amp;, this]</code> is
        redundant but accepted for compatibility with ISO C++ 2014. &ndash; <em>end note</em>]
        Ignoring appearances in initializers of <em>init-captures</em>, an identifier or <code>this</code>
        shall not appear more than once in a <em>lambda-capture</em>. [<em>Example</em>:</p>
      <div class="code">struct S2 { void f(int i); };
        void S2::f(int i) {
        &nbsp; [&amp;, i]{ }; &nbsp; &nbsp; &nbsp; &nbsp; // OK
        &nbsp; <ins>[&amp;, this, i]{ }; &nbsp; // OK, equivalent to [&amp;, i]</ins>
        &nbsp; [&amp;, &amp;i]{ }; &nbsp; &nbsp; &nbsp; &nbsp;// error: i preceded by &amp; when &amp; is the default
        &nbsp; [=, *this]{ }; &nbsp; &nbsp; // OK
        &nbsp; [=, this]{ }; &nbsp; &nbsp; &nbsp;// <del>error: this when = is the default</del><ins>OK, equivalent to [=]</ins>
        &nbsp; [i, i]{ }; &nbsp; &nbsp; &nbsp; &nbsp; // error: i repeated
        &nbsp; [this, *this]{ }; &nbsp;// error: this appears twice
        }</div>
      <p>&ndash; <em>end example</em>]</p>
    </div>

  </body>
</html>
