[LLVMbugs] [Bug 24164] New: Lifetime of temporaries created in init-captures

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Jul 17 05:28:58 PDT 2015


https://llvm.org/bugs/show_bug.cgi?id=24164

            Bug ID: 24164
           Summary: Lifetime of temporaries created in init-captures
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++14
          Assignee: unassignedclangbugs at nondot.org
          Reporter: dyp-cpp at gmx.net
                CC: llvmbugs at cs.uiuc.edu
    Classification: Unclassified

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

http://wg21.cmeerw.net/cwg/issue1695

#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";
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150717/14c8b2d4/attachment.html>


More information about the llvm-bugs mailing list