Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Lambda type not isolated within function

35 views
Skip to first unread message

Frederick Virchanza Gotham

unread,
Apr 20, 2023, 6:06:46 AM4/20/23
to

I realised today that you can create a lambda object outside of the function in which the lambda is defined:

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

return *decltype(&mylambda)(nullptr);
}

typedef decltype(Func()) LambdaType;

int main(void)
{
LambdaType mylambda;

mylambda();
}

And on GodBolt: https://godbolt.org/z/js57TcT1K

Did the Standard Committee intend for this to be impossible?

Bonita Montero

unread,
Apr 20, 2023, 6:16:11 AM4/20/23
to
Am 20.04.2023 um 12:06 schrieb Frederick Virchanza Gotham:
>
> I realised today that you can create a lambda object outside of the function in which the lambda is defined:
>
> auto Func(int a)
> {
> auto mylambda = [](void)->int { return 27u; };
>
> return *decltype(&mylambda)(nullptr);
> }

What's the meaning of returning a null-pointered Lambda ?


Frederick Virchanza Gotham

unread,
Apr 20, 2023, 6:32:06 AM4/20/23
to
On Thursday, April 20, 2023 at 11:16:11 AM UTC+1, Bonita Montero wrote:
>
> What's the meaning of returning a null-pointered Lambda ?


Sometimes we write a function not for what the function actually does, but simply just to get a type from it.

I have no intention of using the return value from an invocation of that function.

Bonita surely you've seen this before where people write functions simply just to use 'decltype' on them later.

Bonita Montero

unread,
Apr 20, 2023, 8:54:16 AM4/20/23
to
Am 20.04.2023 um 12:31 schrieb Frederick Virchanza Gotham:
> On Thursday, April 20, 2023 at 11:16:11 AM UTC+1, Bonita Montero wrote:
>>
>> What's the meaning of returning a null-pointered Lambda ?
>
>
> Sometimes we write a function not for what the function actually does, but simply just to get a type from it.

You coudn't default-construct the lambda from outside, so you
coudn't refer to that type and the pointer does't make sense.

Frederick Virchanza Gotham

unread,
Apr 20, 2023, 8:59:31 AM4/20/23
to
On Thursday, April 20, 2023 at 1:54:16 PM UTC+1, Bonita Montero wrote:

> You coudn't default-construct the lambda from outside, so you
> coudn't refer to that type and the pointer does't make sense.


I can and I did: https://godbolt.org/z/dcvrE138s

Bonita Montero

unread,
Apr 20, 2023, 9:15:50 AM4/20/23
to
Try it when it is relevant: when you have a lambda with a capture.

Bonita Montero

unread,
Apr 21, 2023, 2:14:52 PM4/21/23
to
But default-constructing a non-capturing lamhda needs C++20:

int main()
{
auto toCopy = []() {};
decltype(toCopy) copied; // needs C++20
}

Andrey Tarasevich

unread,
Apr 22, 2023, 2:20:52 PM4/22/23
to
On 04/20/23 3:06 AM, Frederick Virchanza Gotham wrote:
>
> I realised today that you can create a lambda object outside of the function in which the lambda is defined:
>
> auto Func(int a)
> {
> auto mylambda = [](void)->int { return 27u; };
>
> return *decltype(&mylambda)(nullptr);
> }
>

A lot of things became trivially "non-isolated" in C++ the moment the
language started to support `auto` return types and `decltype`
(primarily the former). Local types, `private` types... Yes, this is how
it is intended to work. Get used to it.

--
Best regards,
Andrey
0 new messages