[cfe-dev] No warning when lambda returns reference to temporary
Matthew Urquhart via cfe-dev
cfe-dev at lists.llvm.org
Wed Jul 12 21:55:12 PDT 2017
Hiya,
I ran into this (presumably) undefined behaviour today, and wonder if it's
a bug or expected. My app was coded similarly and ran correctly on
macOS/LLVM version 8.1.0 (clang-802.0.38) but not
Ubuntu14.04/gcc-4.7.3-12ubuntu1
In the case of this code, it's invalid since the lambda returns a reference
to a temp object. My bad, I'll fix it. Seems the compiler could've warned
me though :)
Compiler warns when explicitly specifying the return type of the lambda
using `->` operator, but not when implicit. Is this expected or a bug?
Thanks in advance,
Matt
// Minimal test case:
#include <functional>
#include <iostream>
void print_stuff(std::function<const int&()> f) {
std::cout << "stuff is: " << f() << std::endl;
}
int main(int argc, char *argv[]) {
print_stuff([]() {
int i = 123;
return i;
});
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170713/cfd19d5b/attachment.html>
More information about the cfe-dev
mailing list