Topic: Initialising const members of a struct
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/11/29 Raw View
duncan@rcp.co.uk (Duncan Booth) writes:
>Consider the following code (which is legal C):
> struct S {
> int x;
> const int y;
> };
> struct S s = { 3, 4};
It's legal C++ too, I/m pretty sure.
>MS Visual C++ complains about this [...] is this a bug in Microsoft C++?
Yes. (Incidentally, GNU C++, Sun C++, and SGI C++ all accept this code.)
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: "Bradd W. Szonye" <bradds@concentric.net>
Date: 1996/11/29 Raw View
Duncan Booth <duncan@rcp.co.uk> wrote in article
<57ju5h$lkb@panther.rmplc.co.uk>...
> Consider the following code (which is legal C):
> struct S {
> int x;
> const int y;
> };
> struct S s = { 3, 4};
>
> MS Visual C++ complains about this...
>
> The draft standard says:
> An aggregate is an array or a class with no user-declared constructors,
no
> private or protected non-static data members, no base classes, and no
virtual
> functions.
The April 1995 DWP said differently:
An aggregate [has] no user-declared constructors, no private or protected
non-static data members, no non-static mebers of reference type, no
non-static 'const' members, no base classes, and no virtual functions.
The part about references and const members was removed at least as early
as January 1996. Apparently MSVC++ is using a 1995 definition of aggregate,
and complaining faithfully about a violation of those rules. The April 1995
rules on aggregates and POD-structs were confusing and redundant, and have
changed since then.
--
Bradd W. Szonye
bradds@concentric.net
http://www.concentric.net/~bradds
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: duncan@rcp.co.uk (Duncan Booth)
Date: 1996/11/29 Raw View
In article <01bbddba$8d23a680$ea76adce@azguard>, "Bradd W. Szonye" <bradds@concentric.net> wrote:
>Duncan Booth <duncan@rcp.co.uk> wrote in article
><57ju5h$lkb@panther.rmplc.co.uk>...
>> Consider the following code (which is legal C):
>> struct S {
>> int x;
>> const int y;
>> };
>> struct S s = { 3, 4};
>>
>> MS Visual C++ complains about this...
>>
>> The draft standard says:
>> An aggregate is an array or a class with no user-declared constructors,
>no
>> private or protected non-static data members, no base classes, and no
>virtual
>> functions.
>
>The April 1995 DWP said differently:
>
>An aggregate [has] no user-declared constructors, no private or protected
>non-static data members, no non-static mebers of reference type, no
>non-static 'const' members, no base classes, and no virtual functions.
>
>The part about references and const members was removed at least as early
>as January 1996. Apparently MSVC++ is using a 1995 definition of aggregate,
>and complaining faithfully about a violation of those rules. The April 1995
>rules on aggregates and POD-structs were confusing and redundant, and have
>changed since then.
Thanks. This shows the dangers of only referring to the latest DWP.
However, having trawled through some other bits of the standard I have a
followup question:
Section 7.1.5.1 on cv-qualifiers says:
"There are two cv-qualifiers, const and volatile. If a cv-qualifier
appears in a decl-specifier-seq, the init-declarator-list of the
declaration shall not be empty."
A decl-specifier-seq can appear as part of a member-declaration in which case
there is not necessarily an init-declarator-list anywhere. What, if any,
relevance does this paragraph have to a decl-specifier-seq that is not part of
a simple-declaration?
Is it legitimate to fail to initialise a cv-qualified member of a class? If
not, my example may fail because there is no valid default constructor for the
struct (even though the default constructor is never used).
--
Duncan Booth duncan@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
http://ourworld.compuserve.com/homepages/D_Booth
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: gusty@mail.clark.net (Harlan Messinger)
Date: 1996/11/29 Raw View
duncan@rcp.co.uk (Duncan Booth) wrote:
>Consider the following code (which is legal C):
> struct S {
> int x;
> const int y;
> };
> struct S s = { 3, 4};
>
>MS Visual C++ complains about this when compiled by a C compiler with several
>warnings and one error:
> error C2552: 's' : nonaggregates cannot be initialized with initializer list
Symantec C++ 7.2 for Windows issues a compiler error, " member 'y' is
const but there is no constructor".
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: duncan@rcp.co.uk (Duncan Booth)
Date: 1996/11/28 Raw View
Consider the following code (which is legal C):
struct S {
int x;
const int y;
};
struct S s = { 3, 4};
MS Visual C++ complains about this when compiled by a C compiler with several
warnings and one error:
error C2552: 's' : nonaggregates cannot be initialized with initializer list
The draft standard says:
An aggregate is an array or a class with no user-declared constructors, no
private or protected non-static data members, no base classes, and no virtual
functions.
The structure has none of the disallowed features, so is this a bug in
Microsoft C++? (One of the warnings the compiler generates is that it
was unable to generate a default constructor or assignment operator,
but as far as I can tell that should be irrelevant).
--
Duncan Booth duncan@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
http://ourworld.compuserve.com/homepages/D_Booth
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]