This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/86980] Lambda function with return type rvalue reference dtor issue
- From: "neeraj.sharma at alumni dot iitg.ernet.in" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Thu, 16 Aug 2018 18:06:16 +0000
- Subject: [Bug c++/86980] Lambda function with return type rvalue reference dtor issue
- Auto-submitted: auto-generated
- References: <bug-86980-4@http.gcc.gnu.org/bugzilla/>
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 :)