Reputation: 1310
Looking at the example on the page:
https://en.cppreference.com/w/cpp/algorithm/generate
It uses a lambda:
std::generate(v.begin(), v.end(), [n = 0] () mutable { return n++; });
the variable n
is not declared anywhere prior to the lambda.
Having tried this snippet in MSVC 14 and GCC 9.1, it does work on both.
Try to find a reference to this behaviour at:
https://en.cppreference.com/w/cpp/language/lambda
but cannot find anywhere where it says that new variables can be declared in the capture region of a lambda.
Is this expected behaviour? What are the restrictions if it is?
Upvotes: 2
Views: 655
Reputation: 409482
In the reference you link to, it's in the lambda capture section, item number 3 in the list.
And from further down (in a "since C++14" section):
A capture with an initializer acts as if it declares and explicitly captures a variable declared with type
auto
[Emphasis mine]
Which explicitly explains the behavior of that capture clause.
Upvotes: 6