Topic: Help in interpreting the standard on initializing aggregate types


Author: pnosedikspam@centrum.sk (Peter)
Date: Wed, 23 Jun 2004 17:38:18 +0000 (UTC)
Raw View
> static const struct { const char mem[15]; } init = { "something" };

I tried this code in VC++6.0, and I've got:
error C2552: 'init' : non-aggregates cannot be initialized with
initializer list

The problem is the constness of the member mem. When I write it like
this:
static const struct { char mem[15]; } init = { "something" };

it compiles without warnings. Is this in compliance with the Standard?
(I don't see any mention of constness in 8.5.1).

--
pNOsedikSPAM at centrum dot sk

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: sgganesh@gmail.com (Ganesh)
Date: Thu, 24 Jun 2004 15:32:32 +0000 (UTC)
Raw View
> > static const struct { const char mem[15]; } init = { "something" };
>
> I tried this code in VC++6.0, and I've got:
> error C2552: 'init' : non-aggregates cannot be initialized with
> initializer list

If we treat this as an aggregate type as described in 8.5.1, VC++ 6.0
stands wrong. However, VC7.1 gives only a warning:

warning C4510: '__unnamed' : default constructor could not be
generated
        Source4.cpp(2) : see declaration of '__unnamed'

-Ganesh

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: sgganesh@india.hp.com (Ganesh)
Date: Tue, 15 Jun 2004 17:11:06 +0000 (UTC)
Raw View
Standard Gurus,

For the following:

static const struct { const char mem[15]; } init = { "something" };

Compilers give a warning message in C++ mode (for example, Comeau):

"ComeauTest.c", line 1: warning: class "<unnamed>" defines no
constructor to
          initialize the following:
            const member "<unnamed>::mem"
  static const struct { const char mem[15]; } init = { "something" };
                      ^

Isn't this legal by 8.5.1 (which treats this as a aggregate type that
doesn't require a constructor)? Can I safely assume that 12.6.2[4]
doesn't apply, and compilers issue this warning just to be "friendly"
to the users?

Thanks,
Ganesh.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: usenet@disemia.com (edA-qa mort-ora-y)
Date: Wed, 16 Jun 2004 17:45:21 +0000 (UTC)
Raw View
Ganesh wrote:
> static const struct { const char mem[15]; } init = { "something" };
>
> Isn't this legal by 8.5.1 (which treats this as a aggregate type that
> doesn't require a constructor)? Can I safely assume that 12.6.2[4]
> doesn't apply, and compilers issue this warning just to be "friendly"
> to the users?

I'm not so certain that 12.6.2[4] doesn't apply.  mem is not of class
type so it fails the first bullet point for default construction*, so
the second point seems to apply.  The second point indicates that if mem
is a const qualified type (which it is) then the program is ill-formed.

Though, 8.5.1.9 indicates that an entity of reference type is covered in
aggregate initilization, so this may imply const can also be covered
(which is what I would assume is intended).

To me it appears the standard isn't saying whether your program is
ill-formed or not, though I assume it is will formed since it is allowed
for reference types.

(*Assuming the second part of the first bullet also implies class type)

--
edA-qa mort-ora-y (Producer)
Trostlos Records <http://trostlos.org/>

"What suffering would man know if not for his own?"

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]