Description of Problem: Fails to compile what I believe is valid code. A *declaration* for an array without size specified is being treated like a trailing empty array structure member declaration, i.e. treated as though it were size one. A later *definition* of the array with initialisers is treated as an error. Code that fails to compile: int const foo[]; int main (int ac, char **av) { int const *ip = foo; return 0; } int const foo[] = { 3, 2, 1, 0 }; Version-Release number of selected component (if applicable): gcc-3.1-0.10 How Reproducible: Try to compile the above ... Actual Results: [bill@desktop tmp]$ c++ -o Bug Bug.C Bug.C:2: uninitialized const `foo' Bug.C:2: array size missing in `foo' Bug.C:4: redefinition of `const int foo[]' Bug.C:2: `const int foo[1]' previously declared here Bug.C:4: excess elements in aggregate initializer [bill@desktop tmp]$ Expected Results: [bill@desktop tmp]$ /usr/bin/i386-redhat-linux7-c++ -o Bug Bug.C [bill@desktop tmp]$ Additional Information: Strictly guesswork territory here, but is this to do with allowing an empty array to be declared as the last member of a structure (the variable-size hack) ? I know that compiling with a previous version proves nothing, but this does work with 2.96 :o) and is valid according to the grammar in the back of Stroustrup [3rd].
OK, it should have been the "gcc-c++" package up there ... If this is actually violating the ANSI standard I'd like to know; it doesn't look like it. Basically that error should only be issued if *by the end of the translation unit* the array type has not been completed. Since the element type (int) is complete, it should not be an error to index the array in main(); the incomplete declaration should be equivalent to "extern const int foo[]" (which is accepted).
I was too specific in my intitial report, by the way; the problem exists with or without the "const" qualifier on the array, but there is an extra error when the const is present.
This bug is not valid. If I compile with the Digital Unix C++ compiler, I get: ============================================================================= cxx: Error: Bug.C, line 1: const variable "foo" requires an initializer int const foo[]; ---------------^ cxx: Error: Bug.C, line 1: incomplete type is not allowed int const foo[]; ----------^ cxx: Error: Bug.C, line 2: declaration has already been defined by variable "foo" (declared at line 1) int const foo[] = { 3, 2, 1, 0 }; ----------^ cxx: Info: 3 errors detected in the compilation of "Bug.C". [cjrodrig@is02 /home10/c/j/cjrodrig]% cxx -c Bug.C cxx: Error: Bug.C, line 1: const variable "foo" requires an initializer int const foo[]; ---------------^ cxx: Error: Bug.C, line 1: incomplete type is not allowed int const foo[]; ----------^ cxx: Error: Bug.C, line 2: declaration has already been defined by variable "foo" (declared at line 1) int const foo[] = { 3, 2, 1, 0 }; ----------^ cxx: Info: 3 errors detected in the compilation of "Bug.C". ============================================================================= If the code example is changed to a 2 line example, it works with gcc and Digital Unix C++: int const foo[] = { 3, 2, 1, 0 }; int main (int ac, char **av) { int const *ip = foo; return 0; }
OK *shrug* I know it's valid C ... I couldn't find anything to explain why it should have become illegal in C++. Must have missed something. If you happen to know where in the standard this is standardized (I went through a lot of stuff that Google turned up, and the only impression I came away with was that this ought to be fine ...) I'd appreciate someone telling me. One day I might get to read the thing.
ISO C++ doesn't have tentative definitions (unlike C).
Oh well ... chalk one up to experience. Most of the other things which have caught me out have been obvious violations of something. I couldn't find any reference to this in Stroustrup ... anyway, now, like Coleridge's wedding guest, I am a sadder and wiser man ;o)