Bug 68478 - flexible array members have complete type
Summary: flexible array members have complete type
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: ---
Assignee: Martin Sebor
URL:
Keywords: accepts-invalid
Depends on:
Blocks: flexmembers
  Show dependency treegraph
 
Reported: 2015-11-21 23:47 UTC by Martin Sebor
Modified: 2018-08-28 11:24 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-12-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2015-11-21 23:47:54 UTC
G++ accepts definitions of C flexible array members as an extension.  However, since C specifies that a flexible array member has an incomplete type, such a member is not a valid operand in expressions that require an object to have a complete type.  An example is the sizeof expression.  Since flexible array members are provided for compatibility with C, they should be subject to the same requirements and restrictions.

$ cat z.cpp && /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -Wall -Wextra -Wpedantic -xc++ z.cpp
struct S {
    int n;
    int a[];
} s;

extern int a[];

enum E {
    e = sizeof s.a,   // rejected in C, accepted in C++
    f = sizeof a      // rejected in both C and C++
};

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

z.cpp:10:16: error: invalid application of ‘sizeof’ to incomplete type ‘int []’
     f = sizeof a      // rejected in both C and C++
                ^
Comment 1 Martin Sebor 2015-12-04 04:52:52 UTC
Patch posted for review:
https://gcc.gnu.org/ml/gcc-patches/2015-12/msg00511.html
Comment 2 Martin Sebor 2015-12-17 01:41:32 UTC
Fixed in r231665.