[cfe-dev] Flexible array fields in C++ classes.

Enea Zaffanella zaffanella at cs.unipr.it
Tue Feb 2 07:10:01 PST 2010


When compiling the following C++ code
struct S {
   int size;
   int vec[]; // This is OK.

class C {
   int size;
   int vec[]; // This triggers diagnostic.

clang complains about the flexible array 'vec' that is found at the end 
of class C, whereby it accepts the very same thing if at the end of 
struct S. This is different wrt g++ behavior, which accepts both cases.

$ clang++ -fsyntax-only flex.cc
flex.cc:9:7: error: field has incomplete type 'int []'
   int vec[]; // This triggers diagnostic.
1 diagnostic generated.

Assuming that the one above is not what was really meant,
as far as I can see, the relevant code is in SemaDecl.cpp
(in function Sema::ActOnFields()):
     } else if (FDTy->isIncompleteArrayType() && i == NumFields - 1 &&
                Record && Record->isStruct()) {
       // Flexible array member.
       if (NumNamedMembers < 1) {
         Diag(FD->getLocation(), diag::err_flexible_array_empty_struct)
           << FD->getDeclName();
       // Okay, we have a legal flexible array member at the end of the 
       if (Record)

The impression is that the if-guard condition
is an overkill and could be replaced by the weaker form
     (Record->isStruct() || Record->isClass())

Patch is attached.

Enea Zaffanella.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: flex_array_in_class.patch
Type: text/x-patch
Size: 589 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20100202/9a3d6ce3/attachment.bin>

More information about the cfe-dev mailing list