<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW " title="NEW --- - Lifetime of temporaries created in init-captures" href="https://urldefense.proofpoint.com/v2/url?u=https-3A__llvm.org_bugs_show-5Fbug.cgi-3Fid-3D24164&d=AwMBaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=pF93YEPyB-J_PERP4DUZOJDzFVX5ZQ57vQk33wu0vio&m=tzlArfLkBB_jhZxKi5TjOBqVOrhzp3RE5xqlzkSusos&s=WQ4UGqcx-jmEMFDXDRZqky2Sg9FDF-Y8-YgJhGAInbA&e=">24164</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Lifetime of temporaries created in init-captures
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++14
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>dyp-cpp@gmx.net
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>clang does not extend the lifetime of temporaries which are created in an
init-capture and captured by reference. Using the example from CWG 1695

<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__wg21.cmeerw.net_cwg_issue1695&d=AwMBaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=pF93YEPyB-J_PERP4DUZOJDzFVX5ZQ57vQk33wu0vio&m=tzlArfLkBB_jhZxKi5TjOBqVOrhzp3RE5xqlzkSusos&s=eDJUZ7Af53pNS7Ki0sXSltbTCfmRPNolNueva6s0pD8&e=">http://wg21.cmeerw.net/cwg/issue1695</a>

#include <iostream>
struct S { ~S() { std::cout << "dtor\n"; } };
const S f() { return {}; }
int main() {
    auto &&lambda = [&x(f())] () -> auto& { return x; };
    auto &y = lambda(); // ok?
    std::cout << "end of main\n";
}

The output demonstrates that the temporary created via `f()` is destroyed
before "end of main" is printed. CWG 1695 mentions that "CWG agreed that this
example should extend the lifetime of the temporary", which should extend the
lifetime of the temporary until after the aforementioned print statement.

Moreover, the slightly simpler

#include <iostream>
struct S { ~S() { std::cout << "dtor\n"; } };
const S f() { return {}; }
int main() {
    [&x(f())]{ std::cout << "in lambda\n"; }();
    std::cout << "end of main\n";
}

shows that the dtor of the temporary is called even before the end of the
full-expression which contains the lambda.

While gcc currently has troubles with these examples (e.g. it seems to
incorrectly deduce the constness of `&x`), it keeps the temporary alive until
the end of the full-expression that contains the lambda:

#include <iostream>
struct S { ~S() { std::cout << "dtor\n"; } };
S f() { return {}; }
template<typename T> T& as_lvalue(T&& t) { return t; }
int main() {
    [&x(as_lvalue(f()))]{ std::cout << "in lambda\n"; }();
    std::cout << "end of main\n";
}</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>