[LLVMbugs] [Bug 19231] New: Returning lambdas with dangling references produces no warning

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Sun Mar 23 10:40:43 PDT 2014


            Bug ID: 19231
           Summary: Returning lambdas with dangling references produces no
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: gonzalobg88 at gmail.com
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

In C++14 you can return lambdas from functions directly as well as stored in an
std::function. In both cases, the compiler never warns about dangling
references even in trivial situations (same is true when returning any other
function object with a dangling reference).

This mistake is easy to make and hard to debug. It would be nice if it could be
prevented by Wall.


#include <iostream>

auto f(const int a) {
  const int b = a;
  return [&](){ return b; }; // returns dangling ref inside the lambda

int main() {
  auto i = f(0);
  std::cout << "i: " << i() << "\n";
  return 0;

flags used: -O0 -std=c++1y -Wall -Wextra

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/20140323/570ee9af/attachment.html>

More information about the llvm-bugs mailing list