Topic: Define a static in a class declaration


Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/05/07
Raw View
Steve Clamage wrote:
>
> In article 1@shell7.ba.best.com, ncm@nospam.cantrip.org (Nathan Myers) writes:
> >Fergus Henderson<fjh@cs.mu.OZ.AU> wrote:

> >>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.
>
> As a practical matter, you may be able to get by without a definition
> of the const static data member, but the draft standard says you must
> provide the definition if you use the member.

A more intelligent implementation would automatically generate
these silly definitions which don't carry any information. But
it isn't what the draft says. Oh, sorry, it's nearly an IS now.
Old habits die hard.

--

Valentin Bonnard                mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/


--Herd_of_Walrus_307_000
Content-Type: MESSAGE/rfc822; name=msg2
Content-Description: msg2
---
[ 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: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/05/05
Raw View
In article fsf@zecarneiro.lsd.dcc.unicamp.br, Alexandre Oliva <oliva@dcc.unicamp.br> writes:
>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.

In addition to having integral or enum type, the data member must
be const. Otherwise, no initializer is allowed.

---
Steve Clamage, stephen.clamage@sun.com
---
[ 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: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/05/05
Raw View
In article 0@news.cadvision.com, "sweet boy" <anson@mail.com> writes:
>
>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.

The above explanation is correct as far as it goes, but the C++ draft
standard loosens the requirements somewhat.

If the static data member is const and is also of integral or enumeration
type, you can provide an initializer in the class. The initializer must
be a constant integral expression, and the static member then is also
a constant integral expression. You still need a separate definition,
but in that case the definition must not have an initializer.

Example:

 class C {
 public:
  static const int size = 10;
  T array[size]; // T is some type
 };

 const int C::size; // in just one location, but without an initializer

Because C::size is a constant, you can use it anywhere a constant
is needed.

---
Steve Clamage, stephen.clamage@sun.com
---
[ 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: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1998/05/06
Raw View
In article 1@shell7.ba.best.com, ncm@nospam.cantrip.org (Nathan Myers) writes:
>Fergus Henderson<fjh@cs.mu.OZ.AU> wrote:
>>"Roger Onslow" <Roger_Onslow_nospam@compsys.com.au> writes:
>>>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.

Right.

>>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.

As a practical matter, you may be able to get by without a definition
of the const static data member, but the draft standard says you must
provide the definition if you use the member.

Section 9.4.2 "Static data members", paragraph 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 constant 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."

---
Steve Clamage, stephen.clamage@sun.com
---
[ 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              ]