[cfe-dev] c++ question: can lambda be used in VLA?

Akira Hatanaka via cfe-dev cfe-dev at lists.llvm.org
Wed May 25 16:19:54 PDT 2016


On Tue, May 24, 2016 at 7:11 PM, James Dennett <james.dennett at gmail.com>
wrote:

> On Tue, May 24, 2016 at 6:25 PM, Akira Hatanaka via cfe-dev <
> cfe-dev at lists.llvm.org> wrote:
>
>> I wasn't requesting that clang accept lambda expressions used for array
>> bounds but was asking whether it was valid in c++. Is this something that
>> is open to interpretation because it's not covered by the standard?
>>
>> FYI, this isn't something that I made up. It was in a code a user wrote.
>>
>>
> It's covered by the standard, and as Clang's error message says, lambdas
> are not allowed in constant expressions in C++11 or C++14.
>
>
Yes, the c++ standard gives a list of subexpressions that are not allowed
in constant expressions and lambda expression is one of them.

This doesn't seem to apply to C99's extension for variable length arrays
because array sizes are not required to be constant expressions.

For example, if we declare the following variable length arrays,

(1) unsigned char a1[foo1()];
(2) unsigned char a1[dynamic_cast<S*>(s) + 3];

clang compiles (1) and (2) without any warnings or errors (unless -pedantic
is used). However, if we use lambda for the array size, clang complains
that lambda cannot be used in constant expressions:

(3) unsigned char a1[([](int a){ return a; })(4)];

https://isocpp.org/files/papers/N4487.pdf proposed constexpr lambdas for a
> future standard, and was accepted by EWG; draft wording is in
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0170r1.pdf
>
> -- James
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160525/b367dc16/attachment.html>


More information about the cfe-dev mailing list