https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86980
--- Comment #2 from Neeraj <neeraj.sharma at alumni dot iitg.ernet.in> --- (In reply to Jonathan Wakely from comment #1) > This is a bug in your code, not GCC. you're returning a reference to a local > variable, which goes out of scope, leaving a dangling reference. > I was just playing with alternatives, hence tried return rvalue reference. The larger question still remains, which is what is the return type in case of the following? auto f = []() { Traceable tmp; return std::move(tmp); }; Traceable a = f(); Additionally, the above code retains the instance till the move is complete (see the console output below). // --- [MoveReturn] START [0x7ffe53dec857] ctor Traceable [0x7ffe53dec887] move ctor Traceable, &other = 0x7ffe53dec857 [0x7ffe53dec857] dtor Traceable [0x7ffe53dec887] dtor Traceable // x-- > When you don't give the lambda an explicit return type it returns by value, > so works correctly. > > You should just do: > > auto f = []() { > Traceable tmp; > return tmp; > }; > > This returns by value and can elide the move, so is safer and faster. I agree, but just curious to see possibilities with lambda function :)