Reputation: 9835
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.
Upvotes: 3
Views: 385
Reputation: 170202
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.
Upvotes: 4