http://llvm.org/bugs/show_bug.cgi?id=19231

            Bug ID: 19231
           Summary: Returning lambdas with dangling references produces no
                    warning
           Product: clang
           Version: trunk
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: [email protected]
          Reporter: [email protected]
                CC: [email protected], [email protected]
    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.

Example:

#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.
_______________________________________________
LLVMbugs mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/llvmbugs

Reply via email to