This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re[2]: Member initialization of arrays of constants
- To: gcc-bugs at gcc dot gnu dot org
- Subject: Re[2]: Member initialization of arrays of constants
- From: "Michael Schwendt" <mschwendt at web dot de>
- Date: Mon, 24 Jul 2000 10:48:41 +0200
- Organization: http://freemail.web.de/
[re-sent because of MAPS blocking first attempt]
On Sun, 23 Jul 2000 llewelly@dbritsch.dsl.xmission.com wrote:
>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.
Then it seems, I've found a bug in both Borland C++ 5.5 and Visual C++ 5.0. They throw errors upon compilation of my code examples and further related tests.
In case of brace-enclosed copy-initialization (_dcl.init.aggr_) of an array of constant chars, BC says:
struct C
{
const char c[4];
};
Error: Constant member 'C::A::c' in class without constructors
>In both of your examples, A is an aggregate, and can therefor be
> initialized with a brace-enclosed initializer list. (See 8.5.1)
How's that possible, considering the extended example of a class with default constructor and, for instance, a non-static member constant of aggregate type?
If you say, it _can_ be initialized, shouldn't the following code be ill-formed then? Uninitialized const-qualified types, according to 12.6.1, 4:
#include <iostream.h>
#include <iomanip.h>
struct A
{
const char c[4];
const j;
int i;
A()
{
i = 42;
}
};
void f()
{
A a;
cout << a.i << endl; // okay, a.i is initialized
cout << a.j << endl; // oops?
cout << a.c << endl; // oops?
const A b = { "foo", 1, 2 }; // error
// b' must be initialized by constructor, not by `{...}'
};
>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.
I'd be forced to make each array of constants static. Else I could not perform copy-initialization.
9.2, 4
A member-declarator can contain a constant-initializer only if
it declares a static member (_class.static_) of integral or
enumeration type, see _class.static.data_.
If you mean, that the brace-enclosed initializer-list is a special case which can only initialize static constant member aggregates, this would make sense.
>(If const-qualifed members could not be initialized, there would no
> use for them.)
Well, const-qualified members can be initialized via the ctor's mem-initializer list. But what about member arrays of const-qualified basic types?
>(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.)
Yes. Whenever I add a constructor, I can forget about using the brace-enclosed initializer-list. Hmmm...
Regards,
Mike
_______________________________________________________________________
1.000.000 DM gewinnen - kostenlos tippen - http://millionenklick.web.de
IhrName@web.de, 8MB Speicher, Verschluesselung - http://freemail.web.de