This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Member initialization of arrays of constants
- To: Michael Schwendt <mschwendt at web dot de>
- Subject: Re: Member initialization of arrays of constants
- From: <llewelly at dbritsch dot dsl dot xmission dot com>
- Date: Sun, 23 Jul 2000 11:02:15 -0600 (MDT)
- Cc: gcc-bugs at gcc dot gnu dot org
[snip]
>
> If I'm informed correctly, initialization of member constants
> can only be performed via a ctor's initializer-list. Member arrays
> of constants cannot be initialized like aggregates and cannot be
> initialized via a ctor's initializer-list either.
There is nothing in 8.5.1 [dcl.init.aggr] that disallows initializing
a member array of constant type with a brace-enclosed initializer
list.
In both of your examples, A is an aggregate, and can therefor be
initialized with a brace-enclosed initializer list. (See 8.5.1)
The fact that there are const-qualified types in involved does not
matter in either of your examples; const qualifed types can be
initialized the same way.
(If const-qualifed members could not be initialized, there would no
use for them.)
As for static members, see 9.4.2. Static data members are never
initialized by member-initializer lists. Instead, static data
members are initialized exactly as you demonstrate in your
'Example 2' .
(The rule you are probably thinking of is probably based on 12.6.2/4,
which does not apply to static data members, or to initialization of an
aggregate via a brace-enclosed intializer list.)
>
> Egcs, however, compiles the following code examples fine by
> mistake:
>
>
> Example 1: // ----------------------------------------------------
>
> struct A
> {
> const char a[4];
> int i;
> };
>
> void f()
> {
> A a = { {'T','E','S','T'}, 1 }; // not allowed, but compiles
>
> // This would be okay, if it were an array of non-constants
> // in the struct.
> }
>
>
> Example 2: // ----------------------------------------------------
>
> struct A
> {
> const char a[4];
> int i;
> };
>
> class C
> {
> static const A a;
> };
>
> const A C::a = { 'T','E','S','T', 2 }; // not allowed, but compiles
>
Both of these examples are well-formed, there is not a bug in your
code, or a bug in gcc.
Thank you for your bug report.