This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/86980] Lambda function with return type rvalue reference dtor issue


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 :)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]