Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an array inside a lambda capture

Tags:

c++

c++17

Lambda captures allow us to create new variables, e.g:

auto l = [x = 10]() { };

I know this also works for std::array but what about C style arrays?

To be clear, I don't want to copy or reference an array here. I want to create a new one inside the capture clause.

like image 579
Timo Avatar asked Sep 13 '25 18:09

Timo


1 Answers

It can't work as currently specified for C style arrays. For one, the capture syntax doesn't allow for declarators to compound types. I.e. the following are invalid as a capture

*x = whatever
x[] = whatever
(*x)() = whatever

So we can't "help dictate" how the captured variable's type is supposed to be determined. The capture specification always makes it equivalent to essentially one of the following initialization syntaxes

auto x = whatever
auto x { whatever }
auto x ( whatever )

Now, this initializes x from whatever. This will always involve, in some shape or form, expressions. Since expressions never keep their C array type outside of certain contexts (sizeof, decltype, etc..) due to their decay into pointers, x's type can never be deduced as an array type.

like image 193
StoryTeller - Unslander Monica Avatar answered Sep 16 '25 08:09

StoryTeller - Unslander Monica