https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68490

            Bug ID: 68490
           Summary: error initializing a structure with a flexible array
                    member
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

g++ accepts definitions of flexible array members as an extension for
compatibility with C.  In C mode, as an extension, gcc and compatible compilers
accept initialized definitions of objects of structures containing flexible
array members, provided such objects have static storage duration.  Some of the
compatible compatible (Clang) also accept the extension in C++ mode.

However, as the test case below shows, g++ rejects such uses.  Since flexible
array members are accepted by g++ for compatibility with C (most notably gcc),
it seems that the initialized definitions should be accepted by g++ as well.

$ cat z.cpp && /build/gcc-trunk-svn/gcc/xg++ -B /build/gcc-trunk-svn/gcc -Wall
-Wextra -Wpedantic -O2  -S -o/dev/null z.cpp
struct A {
    int n;
    int a [];
};

struct A foo (void)
{
    static struct A a = { 2, { 1, 0 } };

    return a;
}

z.cpp:3:12: warning: ISO C++ forbids zero-size array ‘a’ [-Wpedantic]
     int a [];
            ^

z.cpp: In function ‘A foo()’:
z.cpp:8:39: error: too many initializers for ‘int [0]’
     static struct A a = { 2, { 1, 0 } };
                                       ^

Reply via email to