Variable length arrays are not permitted in C++

Hi all,

starting to play with Clang++ I notices this (building our Open Source ExactImage), currently on x86_64, T2 Linux:

lib/Colorspace.cc:442:22: error: variable length arrays are not permitted in C++
  uint8_t gray_lookup[vmax];

the code is straight forward variable length array, like:

..
  const int vmax = 1 << bps;
  uint8_t gray_lookup[vmax];

The error already indicates it is intentionally disabled in clang++ for C++

Even old (3.x?) versions of G++ happily accept this.

Is there an std=c99 like option to allow this in Clang++? Maybe it should accept it by default and warn that it is not portable, GNU extension (but I think present in C++0x)?

René

Hello again,

Hi all,

starting to play with Clang++ I notices this (building our Open Source ExactImage), currently on x86_64, T2 Linux:

lib/Colorspace.cc:442:22: error: variable length arrays are not permitted in C++
uint8_t gray_lookup[vmax];

the code is straight forward variable length array, like:

..
const int vmax = 1 << bps;
uint8_t gray_lookup[vmax];

The error already indicates it is intentionally disabled in clang++ for C++

Even old (3.x?) versions of G++ happily accept this.

Is there an std=c99 like option to allow this in Clang++? Maybe it should accept it by default and warn that it is not portable, GNU extension (but I think present in C++0x)?

René

This trivial changes makes some quick test work:

--- tools/clang/lib/Sema/SemaType.cpp (revision 96337)
+++ tools/clang/lib/Sema/SemaType.cpp (working copy)
@@ -686,7 +686,7 @@
     T = Context.getConstantArrayType(T, ConstVal, ASM, Quals);
   }
   // If this is not C99, extwarn about VLA's and C99 array size modifiers.
- if (!getLangOptions().C99) {
+ if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) {
     if (ArraySize && !ArraySize->isTypeDependent() &&
         !ArraySize->isValueDependent() &&
         !ArraySize->isIntegerConstantExpr(Context))

I'm aware that this is not apply-worthy code, ...

VLAs are disabled until someone goes through and makes sure they work
properly and that we give appropriate errors for C++-only constructs.

-Eli

Hi,

Anything that works in C probably works in C++ as well; it gets
complicated when we try to deal with constructs like VLAs with
non-trivial destructors, using a VLA with new, attempting to use a
variably modified type as a template argument, attempting to use a
variably modified type inside a local class, etc. The idea is that if
we're going to support VLAs inside of C++, we should make sure they
work correctly in every situation, not just in trivial cases.

-Eli

There is already a bug tracking this:

http://llvm.org/bugs/show_bug.cgi?id=5678

There's a patch attached to it for handling VLAs with just POD types. Handling them with other types sounds like it would be quite complex. I'm not sure if this even works reliably with GCC either - I've heard some complaints that it doesn't, but they might have been from older versions of GCC.

As this is a GCC extension, presumably our priority is compatibility with existing code, rather than implementing every possible case, so only supporting POD types might be Good Enough™, as long as we emit a helpful error if someone tries to use VLAs with non-POD types.

David

-- Sent from my Cray X1

Hmm, that's a reasonable restriction, but some of the stuff I
mentioned needs to be dealt with anyway.

-Eli

I would be perfectly happy to have Clang produce errors for all of these cases, so that VLAs in C++ are restricted to the places where they work in C. A patch that correctly restricts VLAs to that subset, and downgrades the "variable length arrays are not permitted in C++" error to an ExtWarn, will be accepted.

  - Doug

Still, shouldn't the message be "not supported yet" rather than "not permitted"?

cheers,
--renato

http://systemcall.org/

Reclaim your digital rights, eliminate DRM, learn more at
http://www.defectivebydesign.org/what_is_drm

It’s an g++ extension rather than a C++ standard feature, hence why “not permitted” is applicable.

Indeed, not even in c++0x, my bad. Then maybe a hint to use dynamic
arrays or vectors? :wink: