Topic: Initialising struct with const member?
Author: alan@rcp.co.uk (Alan Stokes)
Date: 1995/07/18 Raw View
Is this legal in C++, according to the current version of the standard?
struct TEST { const int Value; };
TEST Test = { 1 };
I can't see amything wrong with it, but one compiler (MSVC 2.0) complains
that a default constructor and assignment operator can't be created (because
of the const member), and that therefore no object of type TEST can be
instantiated.
I can't see why a default constructor couldn't be created, and in any case
I thought assignment of a simple struct like this from an initialiser list
bypassed the constructor mechanism altogether.
If this isn't legal, it seems a gratuitous incompatibility with C where this
is (IMHO) legal (assuming the second TEST is changed to struct TEST, of
course).
--
Alan Stokes (alan@rcp.co.uk)
RCP Consultants Ltd
Didcot, UK
Author: fwai@armltd.co.uk (Francis Wai)
Date: 1995/07/19 Raw View
alan@rcp.co.uk (Alan Stokes) writes:
>Is this legal in C++, according to the current version of the standard?
> struct TEST { const int Value; };
> TEST Test = { 1 };
>I can't see amything wrong with it, but one compiler (MSVC 2.0) complains
>that a default constructor and assignment operator can't be created (because
>of the const member), and that therefore no object of type TEST can be
>instantiated.
There is no need for default ctor, op= etc. The compiler fails to recognise
the struct in question is a POD i.e. Plain 'O Data.
-Francis
--
e-mail: fwai@armltd.co.uk
Author: jsa@edg.com (J. Stephen Adamczyk)
Date: 1995/07/19 Raw View
In article <VADDwAZGBh107h@rcp.co.uk> alan@rcp.co.uk (Alan Stokes) writes:
>Is this legal in C++, according to the current version of the standard?
>
> struct TEST { const int Value; };
> TEST Test = { 1 };
>
>I can't see amything wrong with it, but one compiler (MSVC 2.0) complains
>that a default constructor and assignment operator can't be created (because
>of the const member), and that therefore no object of type TEST can be
>instantiated.
There's been a certain fuzziness about this issue for several years. The
ARM said, in 12.6.2 (p. 291), talking about ctor-initializers, "This is
the only way to initialize nonstatic const and reference members."
Some implementers have interpreted this as meaning that classes with
such members must have constructors, because otherwise the const and/or
reference members would be uninitialized. More recently, it's been
noted that aggregate initialization (as shown above) is another way of
initializing such members, and the comment no longer appears in the WP.
This just moves the problem to another section, namely 8.5.1, the definition
of "aggregate". You can brace-initialize something if it is an aggregate.
In the version of the WP sent out for public review, the definition of
aggregate included "no non-static members of reference type, no non-static
const members". This was removed by vote of the committee at the Monterey
meeting last week. So as of this week :-), the example given above is
valid.
Steve Adamczyk
Edison Design Group