This code fails to compile with MSVC but compiles with GCC and CLANG. The problem appears to be that capture by value is not a compile time const in MSVC.
#include <array>
int main()
{
const int n = 41;
auto lambda = [n] { // Both n and nn should be compile time const
const int nn {n+1};
std::array<int,nn> h{};
return h;
};
auto xarray = lambda();
return xarray.size();
}
Error: error C2971: 'std::array': template parameter '_Size': 'nn': a variable with non-static storage duration cannot be used as a non-type argument
Here's a C++20 standard discussion of lambda value capture showing compile time evaluation:
//[Example 6:
void f1(int i) {
int const N = 20;
auto m1 = [=]{
int const M = 30;
auto m2 = [i]{
int x[N][M]; // OK: N and M are not odr-used
x[0][0] = i; // OK: i is explicitly captured by m2 and implicitly captured by m1
};
};
};
So this is a MSVC bug? Sure looks like it to me.