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 14:32:14 +0300
On Thu, 20 Apr 2023 at 14:09, Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> Was it the intention of the Committee from the outset that it should
> not be possible to create a lambda object outside of the function in
> which the lambda type is defined?

No.

>See the following two snippets:
>
> auto Func(void)
> {
> auto mylambda = [](void)->int { return 27u; };
>
> return *decltype(&mylambda)(nullptr);

Don't dereference null pointers, it's undefined behavior to do so.

> And also:
>
> template<typename T>
> void Func(T &arg)
> {
> T obj;
> }
>
> int main(void)
> {
> auto mylambda = [](void)->int { return 27u; };
>
> Func(mylambda);
> }
>
> Was this kind of thing not supposed to be done? Or is it okay?

It's perfectly okay.

Received on 2023-04-20 11:32:27