clang currently errors out when a lambda expression is used to compute the size of an array even though clang supports variable-length arrays as a C99 extension. For example,
$ cat vla1.cpp
int foo3(); struct S1 { virtual ~S1(); }; struct S2 : S1 { }; void foo(S1 *s1) { unsigned char a1[([](int a) {return a; })(1)]; unsigned char a2[foo3()]; // This is OK. unsigned char a3[!dynamic_cast<S2 *>(s1) + 1]; // This is OK. }
$ clang++ vla1.cpp -c -std=c++11
vla1.cpp:11:21: error: a lambda expression may not appear inside of a constant expression
unsigned char a1[([](int a) {return a; })(1)]; ^
1 error generated.
To handle VLAs in a more consistent way, this patch makes changes to allow lambda expressions to be used for array bounds if it is in a BlockContext.
This isn't correct; you still need to produce the diagnostic even if we're in an array bound, but it should be an ExtWarn controlled by -Wvla. A case like
is ill-formed in standard C++, but as we can evaluate the array bound as a constant, Clang will no longer diagnose it with this change in place.