Topic: Define a static in a class declaration .. Legal ??
Author: jkanze@otelo.ibmmail.com
Date: 1998/05/20 Raw View
In article <ork97knnjw.fsf@zecarneiro.lsd.dcc.unicamp.br>,
Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
>
> Bill Gibbons <bill@gibbons.org> writes:
>
> > In article <org1ij41sx.fsf@zecarneiro.lsd.dcc.unicamp.br>, Alexandre
Oliva
> > <oliva@dcc.unicamp.br> wrote:
>
> >> Nathan Myers <ncm@nospam.cantrip.org> writes:
> >> > It must be static, it must be const, and it must be an integer
> >> > or enumeration type.
>
> >> Nope, it needs not be const.
>
> > Yes, it must. That was explicit in the proposal. (I know, I wrote it.)
>
> Does this mean the FDIS's wording for [class.mem]/4 is any different
> from the wording in the Nov'97 DWP, that I've quoted in
>
<URL:http://x3.dejanews.com/getdoc.xp?AN=352730637&CONTEXT=895470201.85655562
6&hitnum=10>
> ? Or is my interpretation incorret?
It almost looks like there is a contradiction in the standard:
[class.mem]/4 references [class.static.data], where it says: "If a
static data member is of const integral or const enumeration type,
its declaration in the class definition can specify a
constant-initializer [...]". The types must be const, although it
doesn't say that in [class.mem]/4.
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
+49 (0)69 66 45 33 10 mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient e objet --
-- Beratung in objektorientierter Datenverarbeitung
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/05/21 Raw View
Bill Gibbons <bill@gibbons.org> writes:
>> 4 A member-declarator can contain a constant-initializer only if it
>> declares a static member (9.4) of integral or enumeration
>> type, see 9.4.2.
> It is true that the restriction is omitted from the second paragraph.
I'm not a native speaker of English, but the text seems quite clear to
me, and 9.4.2/4 seems to specify only an additional property of static
members of integral or enumeration constant types, namely, that they
can be used to form constant expressions. If 9.4.2/4 started with
`If, and only if', or 9.2/4 explicitly restricted to constant types,
there'd be no doubt, but the current specification *is* misleading. A
defect report should be issued, IMO.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/05/18 Raw View
Bill Gibbons <bill@gibbons.org> writes:
> In article <org1ij41sx.fsf@zecarneiro.lsd.dcc.unicamp.br>, Alexandre Oliva
> <oliva@dcc.unicamp.br> wrote:
>> Nathan Myers <ncm@nospam.cantrip.org> writes:
>> > It must be static, it must be const, and it must be an integer
>> > or enumeration type.
>> Nope, it needs not be const.
> Yes, it must. That was explicit in the proposal. (I know, I wrote it.)
Does this mean the FDIS's wording for [class.mem]/4 is any different
from the wording in the Nov'97 DWP, that I've quoted in
<URL:http://x3.dejanews.com/getdoc.xp?AN=352730637&CONTEXT=895470201.856555626&hitnum=10>
? Or is my interpretation incorret?
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: bill@gibbons.org (Bill Gibbons)
Date: 1998/05/18 Raw View
In article <ork97knnjw.fsf@zecarneiro.lsd.dcc.unicamp.br>, Alexandre Oliva
<oliva@dcc.unicamp.br> wrote:
> Does this mean the FDIS's wording for [class.mem]/4 is any different
> from the wording in the Nov'97 DWP? Or is my interpretation incorret?
The wording has not changed; it is still as quoted:
> First, there's 9.4.2/4, that says:
>
> 4 If a static data member is of const integral or const enumeration
> type, its declaration in the class definition can specify a constant-
> initializer which shall be an integral constant expression
> (5.19). In that case, the member can appear in integral con-
> stant expressions within its scope. The member shall still be defined
> in a namespace scope if it is used in the program and the namespace
> scope definition shall not contain an initializer.
>
> Which means that the declaration of a const-qualified static data
> member of integral or enumeration type can have an initializer and, if
> it does, it can be used as a constant.
>
> Note, however, that it doesn't state that an initializer is acceptable
> only for such types. But then, there's 9.2/4:
>
> 4 A member-declarator can contain a constant-initializer only if it
> declares a static member (9.4) of integral or enumeration
> type, see 9.4.2.
It is true that the restriction is omitted from the second paragraph.
When interpreting the draft, though, keep in mind that:
(1) Sections which present an overview of a language feature cannot
quote the entire text of the corresponding detail sections.
In this case, the complete description of static data members
is in 9.4.2, and the overview in 9.2 is less complete - and
less definitive. Obviously one additional word in the overview
would have eliminated some confusion in this case.
(2) The fact that one section does not mention a feature or restriction
does not imply that no such feature or restriction exists; only
its omission throughout the draft would imply that. So when one
section omits a feature or restriction and another specifies it
explicitly, there is no conflict.
The committee was painfully aware that the draft is a very large document
which is very difficult to read. Mentioning every feature only once would
have made the draft more precise but also much less readable. So we tried
to strike a balance, repeating parts of the descriptions of some language
features when it seemed necessary for clarity.
Since these repeated descriptions are usually more brief than the complete
specifications from which they were extracted, they usually omit many of
the details. This often leads to the situation above, where you really
must refer to the defining section to get the complete specification.
(There is usually a cross-reference to that section, as above.)
-- Bill Gibbons
bill@gibbons.org
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/05/11 Raw View
Nathan Myers <ncm@nospam.cantrip.org> writes:
> No, it's not correct. However, you can say this:
> class X { static const int myvalue = 1; };
> ^^^^^
> It must be static, it must be const, and it must be an integer
> or enumeration type.
Nope, it need not be const. You're mixing up two different issues.
First, there's [class.static.data]/4, that says:
4 If a static data member is of const integral or const enumeration
type, its declaration in the class definition can specify a constant-
initializer which shall be an integral constant expression
(_expr.const_). In that case, the member can appear in integral con-
stant expressions within its scope. The member shall still be defined
in a namespace scope if it is used in the program and the namespace
scope definition shall not contain an initializer.
Which means that the declaration of a const-qualified static data
member of integral or enumeration type can have an initializer and, if
it does, it can be used as a constant.
Note, however, that it doesn't state that an initializer is acceptable
only for such types. But then, there's [class.mem]/4:
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_.
Which says nothing about constness.
>> However, you still need a definition
>> somewhere.
> This is also not right, for the cases where in-class definition
> is permitted. You only need such a definition if some bit of
> code takes its address.
It may be the case for most reasonable implementations, but it is not
specified in the (to-be) Standard, so you can't count on this. Better
be on the safe side, and provide a definition somewhere.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/05/11 Raw View
In comp.std.c++ Nathan Myers <ncm@nospam.cantrip.org> wrote:
: Fergus Henderson<fjh@cs.mu.OZ.AU> wrote:
[...]
: class X { static const int myvalue = 1; };
[...]
: >However, you still need a definition
: >
: > static int X::myvalue;
: >
: >somewhere.
: This is also not right, for the cases where in-class definition
: is permitted. You only need such a definition if some bit of
: code takes its address.
Can you point to the wording in the standard which says so?
As a practical matter, I understand that this should be
the case, since anyone who uses the value of the static
member, must see the definition of the class and therefore
the initializer for this static member. However, the standard
seems to say that you must provide the definition nevertheless.
9.4.2 Static data members.
[...]
5 There shall be exactly one definition of a static
data member that is used in the program; no diagnostic
is required; see 3.2. ....
3.2 One definition rule.
[...]
2 .... An object ... is used if its name appears in a
potentially-evaluated expression.
Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/05/13 Raw View
Oleg Zabluda <zabluda@math.psu.edu> wrote:
>In comp.std.c++ Nathan Myers <ncm@nospam.cantrip.org> wrote:
>: Fergus Henderson<fjh@cs.mu.OZ.AU> wrote:
>: class X { static const int myvalue = 1; };
>: >However, you still need a definition
>: > static int X::myvalue;
>: >somewhere.
>
>: You only need such a definition if some bit of
>: code takes its address.
>
>Can you point to the wording in the standard which says so?
I thought I recalled a proposal passing that relaxed the
requirement as described. Unless some wording turns up,
it seems I misremembered.
--
Nathan Myers
ncm@nospam.cantrip.org http://www.cantrip.org/
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: bill@gibbons.org (Bill Gibbons)
Date: 1998/05/15 Raw View
In article <6jbada$3u4$1@shell7.ba.best.com>, ncm@nospam.cantrip.org
(Nathan Myers) wrote:
> Oleg Zabluda <zabluda@math.psu.edu> wrote:
> >In comp.std.c++ Nathan Myers <ncm@nospam.cantrip.org> wrote:
> >: Fergus Henderson<fjh@cs.mu.OZ.AU> wrote:
> >: class X { static const int myvalue = 1; };
> >: >However, you still need a definition
> >: > static int X::myvalue;
> >: >somewhere.
> >
> >: You only need such a definition if some bit of
> >: code takes its address.
> >
> >Can you point to the wording in the standard which says so?
>
> I thought I recalled a proposal passing that relaxed the
> requirement as described. Unless some wording turns up,
> it seems I misremembered.
Yes, we agreed to this, but it appears that the wording did not make it
into the final draft. It seems likely that the official interpretation
of the standard will be that the definition is needed only if the static
member is used as an lvalue, i.e. its address is taken.
In article <org1ij41sx.fsf@zecarneiro.lsd.dcc.unicamp.br>, Alexandre Oliva
<oliva@dcc.unicamp.br> wrote:
> Nathan Myers <ncm@nospam.cantrip.org> writes:
>
> > No, it's not correct. However, you can say this:
>
> > class X { static const int myvalue = 1; };
> > ^^^^^
>
> > It must be static, it must be const, and it must be an integer
> > or enumeration type.
>
> Nope, it need not be const.
Yes, it must. That was explicit in the proposal. (I know, I wrote it.)
-- Bill Gibbons
bill@gibbons.org
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/05/04 Raw View
Roger Onslow <Roger_Onslow_nospam@compsys.com.au> writes:
> Define a static in a class declaration .. Legal ??
Yes, as long as the data member is of integral or enumeration type
[class.mem]/4 and [class.static.data]/4. However, there must still be
a definition of this data member outside the class body, without an
initializer.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: ncm@nospam.cantrip.org (Nathan Myers)
Date: 1998/05/05 Raw View
Fergus Henderson<fjh@cs.mu.OZ.AU> wrote:
>"Roger Onslow" <Roger_Onslow_nospam@compsys.com.au> writes:
>You can't *define* a static in a class declaration; however, if the
>static has integral type, then you can include an initializer for it.
>See 9.2 [class.member] paragraph 4 and 9.4.2 [class.static.data]
>paragraph 5 in the draft standard.
>
>>Someone has suggested that:
>>
>> class X { static int myvalue = 1; };
>>
>>is [allowed by] the CURRENT ANSI C++ [Draft] standard.
>
>Yes, that's correct.
No, it's not correct. However, you can say this:
class X { static const int myvalue = 1; };
^^^^^
It must be static, it must be const, and it must be an integer
or enumeration type.
>However, you still need a definition
>
> static int X::myvalue;
>
>somewhere.
This is also not right, for the cases where in-class definition
is permitted. You only need such a definition if some bit of
code takes its address.
--
Nathan Myers
ncm@nospam.cantrip.org http://www.cantrip.org/
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Roger Onslow" <Roger_Onslow_nospam@compsys.com.au>
Date: 1998/05/02 Raw View
Define a static in a class declaration .. Legal ??
Someone has suggested that:
class X
static int myvalue = 1;
};
is part of the CURRENT ANSI C++ standard.
I thought it was still the case that you needed
class X
static int myvalue;
};
int X::myvalue = 1;
Can anyone confirm/deny this?
If it is not part of the current standard, is such a (sensible) extension
part of any draft standard?
Thanks
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Roger Onslow" <Roger_Onslow_nospam@compsys.com.au>
Date: 1998/05/03 Raw View
Define a static in a class declaration .. Legal ??
Someone has suggested that:
class X
static int myvalue = 1;
};
is part of the CURRENT ANSI C++ standard.
I thought it was still the case that you needed
class X
static int myvalue;
};
int X::myvalue = 1;
Can anyone confirm/deny this?
If it is not part of the current standard, is such a (sensible) extension
part of any draft standard?
Thanks
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: fjh@cs.mu.OZ.AU (Fergus Henderson)
Date: 1998/05/03 Raw View
"Roger Onslow" <Roger_Onslow_nospam@compsys.com.au> writes:
>Define a static in a class declaration .. Legal ??
You can't *define* a static in a class declaration; however, if the
static has integral type, then you can include an initializer for it.
See 9.2 [class.member] paragraph 4 and 9.4.2 [class.static.data]
paragraph 5 in the draft standard.
>Someone has suggested that:
>
>class X
>
> static int myvalue = 1;
>};
>
>is part of the CURRENT ANSI C++ standard.
Yes, that's correct.
However, you still need a definition
static int X::myvalue;
somewhere.
>I thought it was still the case that you needed
>
>class X
>
> static int myvalue;
>};
>
>int X::myvalue = 1;
Nope.
--
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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "sweet boy" <anson@mail.com>
Date: 1998/05/03 Raw View
The former case:
class x
{
static int myvalue = 1;
};
is illegal, because when you declare a static data member within a
class, you're NOT defining it; that is, you're not allocating storage
for it. Instead, you must provide a global definition for the static
data member elsewhere, outside the class, as you did in the later case.
As a convenience, older versions of C++ did not require the second
declaration of a static member variable. However, this convenience gave
rise to serious inconsistencies, and it was eliminated several years
ago. Even so, you may still find older C++ code that does not redeclare
static member variables. In these cases, you'll need a definition;
maybe what someone suggested is because of this.
sweet boy
e-mail: anson@mail.com
ICQ UIN: 2056592
homepage: http://www.geocities.com/~foreverland
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "sweet boy" <anson@mail.com>
Date: 1998/05/04 Raw View
This is a multi-part message in MIME format.
------=_NextPart_000_060D_01BD7658.468BAB40
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
The former case:
class x
{
static int myvalue =3D 1;
};
is illegal, because when you declare a static data member within a =
class, you're NOT defining it; that is, you're not allocating storage =
for it. Instead, you must provide a global definition for the static =
data member elsewhere, outside the class, as you did in the later case.
As a convenience, older versions of C++ did not require the second =
declaration of a static member variable. However, this convenience gave =
rise to serious inconsistencies, and it was eliminated several years =
ago. Even so, you may still find older C++ code that does not redeclare =
static member variables. In these cases, you'll need a definition; =
maybe what someone suggested is because of this.
--=20
sweet boy
e-mail: anson@mail.com
ICQ UIN: 2056592
homepage: http://www.geocities.com/~foreverland
------=_NextPart_000_060D_01BD7658.468BAB40
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">
<HTML>
<HEAD>
<META content=3Dtext/html;charset=3Diso-8859-1 =
http-equiv=3DContent-Type>
<META content=3D'"MSHTML 4.72.2106.6"' name=3DGENERATOR>
</HEAD>
<BODY>
<DIV>
<DIV><FONT color=3D#000000 face=3D"" size=3D2>The former =
case:</FONT></DIV>
<BLOCKQUOTE>
<DIV><EM>class x</EM></DIV>
<DIV><EM>{</EM></DIV>
<DIV><EM> static int myvalue =3D 1;</EM></DIV>
<DIV><EM>};</EM></DIV></BLOCKQUOTE>
<DIV>is illegal, because when you declare a static data member within a =
class,=20
you're <FONT color=3D#ff0000>NOT</FONT> defining it; that is, you're not =
allocating storage for it. Instead, you must provide a global =
definition=20
for the static data member elsewhere, outside the class, as you did in =
the later=20
case.</DIV>
<DIV> </DIV>
<DIV>As a convenience, older versions of C++ did not require the second=20
declaration of a static member variable. However, this convenience =
gave=20
rise to serious inconsistencies, and it was eliminated several years =
ago. =20
Even so, you may still find older C++ code that does not redeclare =
static member=20
variables. In these cases, you'll need a definition; maybe what =
someone=20
suggested is because of this.</DIV></DIV>
<DIV><BR>-- <BR>sweet boy<BR>e-mail: <A=20
href=3D"mailto:anson@mail.com">anson@mail.com</A><BR>ICQ UIN: =
2056592<BR>homepage:=20
<A=20
href=3D"http://www.geocities.com/~foreverland">http://www.geocities.com/~=
foreverland</A><BR></DIV></BODY></HTML>
------=_NextPart_000_060D_01BD7658.468BAB40--
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1998/05/04 Raw View
Roger Onslow <Roger_Onslow_nospam@compsys.com.au> wrote:
: Define a static in a class declaration .. Legal ??
: Someone has suggested that:
: class X {
: static int myvalue = 1;
: };
: is part of the CURRENT ANSI C++ standard.
: I thought it was still the case that you needed
: class X {
: static int myvalue;
: };
: int X::myvalue = 1;
: Can anyone confirm/deny this?
It's in the current standard. You still can not define static in
class declaration, but you can initialize it there, as long as
the static is of constant integral type.
class X {
static int myvalue1 = 1; // illegal. non-const.
static const float myvalue2 = 1; // illegal. non-integral.
static const int myvalue3 = 1; // legal.
};
static const int X::myvalue3; // you still must provide exactly one
// definition.
Oleg.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]