C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Lambda type not isolated within function

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Thu, 20 Apr 2023 16:15:59 +0300
On Thu, 20 Apr 2023 at 15:47, Frederick Virchanza Gotham
<cauldwell.thomas_at_[hidden]> wrote:
> In my previous post I had a function that was only ever defined in
> order to use 'decltype' on it, and so whether or not its contents
> would exhibit undefined behaviour doesn't matter.
>
> Or am I missing something?
>
> By the way I originally used 'declval' instead of dereferencing a
> nullptr but I got a compiler error because GNU g++ doesn't allow you
> to evaluate declval.

Well, gee, all you need to do is

auto Func(void)
{
    auto mylambda = [](void)->int { return 27u; };

    return mylambda; // no *decltype(&mylambda)(nullptr),
overcomplicated to the hilt
}

> > > Was this kind of thing not supposed to be done? Or is it okay?
> > It's perfectly okay.
> This might have implications for my solution of creating thunks by
> monitoring the frame pointer.

Lambdas were copyconstructible ever since they were introduced, and we
have since allowed
them to be defaultconstructible as well when there's nothing
preventing that, i.e. no captures,
as that has rather reasonable use cases. With auto or decltype, you
can return objects
of a locally-defined class type, and lambdas are no different in that
regard, and you were always
able to pass such things into function templates and create new
objects in such function templates, even in C++98.

Received on 2023-04-20 13:16:12