In this answer I use this code:
std::vector<std::vector<int>> imat(3, std::vector<int>(10));
std::for_each(imat.begin(), imat.end(), [&](auto& i) {
static auto row = 0;
auto column = 0;
std::transform(i.begin(), i.end(), i.begin(),
[&](const auto& /*j*/) {
return row * column++;
});
++row;
});
But I notice some misbehavior in capturing static auto row
depending upon the compiler.
0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
And Visual Studio 2015 gives me a compile time error:
An internal error has occurred in the compiler.
If I change the capture nested capture to capture row
explicitly I get the compiler error:
identifier in capture must be a variable with automatic storage duration declared in the reaching scope of the lambda
Am I allowed to capture a static
in a nested lambda? It seems legit, but there are so many problems!
EDIT:
Fozi pointed out that I can get Visual Studio 2015 to compile and give the same output as Clang 3.7.0 if I change the nested lambda's parameter type from const auto&
to const int&
. Which seems completely unrelated, but it works.
This doesn't work if I try to capture row
explicitly. In that case I still get the compiler error:
identifier in capture must be a variable with automatic storage duration declared in the reaching scope of the lambda
I've reported a Visual Studio 2015 bug here: https://connect.microsoft.com/VisualStudio/feedback/details/1930409/capturing-a-lambdas-static-in-a-nested-lambda