Topic: const static member in a class


Author: Ole Reinartz <ole.reinartz@gmx.de>
Date: 2000/05/31
Raw View

Francis Glassborow wrote:
[snip]
(snipped text acknowledged; you are right Francis, it only works as long as
nobody wants to have the address of that const)

> >(like it would do when you would #define m_a 1). Still
> >the type savety remains.
> >Also I think (didnt look it up in the standard though) that it works for all
> >the builtin types.
>
> You really should check the standard. A special rule was allowed for
> static const integer values because such values are often wanted as
> compile time constants (e.g. for an array size). enums were being used
> for named integer literals before this change was made.

Could you tell me where that is in the standard please? I only have the draft
from end of '96. There I cannot find anything about a special rule for static
const integer values. Furthermore the compiler I use (gcc of several versions;
2.7.2 to 2.95) allow me to in- class- initialize static const values of
char,short,long,float and double.

Thanks,
    Ole

---
[ 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: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/05/28
Raw View
Ole Reinartz wrote:
>
> Francis Glassborow wrote:
>
> > In article <3908CCBC.E02307C@hotmail.com>, Yaxun Liu
> > <yx_liu@hotmail.com> writes
> > >
> > >class A
> > >{
> > >public:
> > >static const int m_a=1;
> > >};
> > >
> >
> > This one is not really MS's fault as in class initialisation of static
> > const data (integer types only) was a late tweak to the Standard.  In
> > addition you still have to define the variable exactly once somewhere in
> > your program (for the linker)
> >
>
> AFAIK in that special case you dont even have to define it for the linker
> somewhere, because it is a constant. Because it will never change it doesnt
> have to have a fixed location.

Suppose you take its address -- explicitly, or by passing it via a
reference parameter.  It then needs a location in memory.  The
compiler can't just create a temp on the fly, because if you take its
address in different translation units, the addresses must compare
equal (it has external linkage).

If you don't "use" the member, it doesn't need a definition. Otherwise,
the standard says the results are undefined unless you provide exactly
one definition in the program -- the same as for any object with
external linkage.

--
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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/05/29
Raw View
In article <392E5925.6BABB160@gmx.de>, Ole Reinartz
<ole.reinartz@gmx.de> writes
>AFAIK in that special case you dont even have to define it for the linker
>somewhere, because it is a constant. Because it will never change it doesnt
>have to have a fixed location. The compiler is even allowed to optimize it
>to a literal constant

NO, it is only allowed to do this if it can prove that you never use the
address of the storage.  The following code is perfectly legitimate:

int const i = 3;
int const * i_ptr = &i;

The compiler can only optimise away the storage if nothing in the
program will detect this. Of course the compiler can optimise all uses
of the value but that is not the same thing as not providing storage.

As I wrote before, it would take a very meticulous compiler to complain
that you had not defined the storage if you never actually used the
address of such storage. If, in the case in point, you make the static
const  data private it is relatively easy for the compiler to determine
that the address is not used, but if you assume that public static const
data is fine, you just shot the optimiser.

>(like it would do when you would #define m_a 1). Still
>the type savety remains.
>Also I think (didnt look it up in the standard though) that it works for all
>the builtin types.

You really should check the standard. A special rule was allowed for
static const integer values because such values are often wanted as
compile time constants (e.g. for an array size). enums were being used
for named integer literals before this change was made.

Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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: Ole Reinartz <ole.reinartz@gmx.de>
Date: 2000/05/26
Raw View
Francis Glassborow wrote:

> In article <3908CCBC.E02307C@hotmail.com>, Yaxun Liu
> <yx_liu@hotmail.com> writes
> >Hi,
> >  I wonder whether the following codes satisfy the C++ standard,
> >
> >class A
> >{
> >public:
> >static const int m_a=1;
> >};
> >
> >I've seen some people used such codes, however, it caused compling error
> >
> >in VC++5.0. I was rather confused.
>
> This one is not really MS's fault as in class initialisation of static
> const data (integer types only) was a late tweak to the Standard.  In
> addition you still have to define the variable exactly once somewhere in
> your program (for the linker)
>

AFAIK in that special case you dont even have to define it for the linker
somewhere, because it is a constant. Because it will never change it doesnt
have to have a fixed location. The compiler is even allowed to optimize it
to a literal constant (like it would do when you would #define m_a 1). Still
the type savety remains.
Also I think (didnt look it up in the standard though) that it works for all
the builtin types.

Ole

---
[ 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: Yaxun Liu <yx_liu@hotmail.com>
Date: 2000/04/27
Raw View
Hi,
  I wonder whether the following codes satisfy the C++ standard,

class A
{
public:
static const int m_a=1;
};

I've seen some people used such codes, however, it caused compling error

in VC++5.0. I was rather confused.

Thanks in advance.
Yaxun

---
[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 2000/04/28
Raw View
In article <3908CCBC.E02307C@hotmail.com>, Yaxun Liu
<yx_liu@hotmail.com> writes
>Hi,
>  I wonder whether the following codes satisfy the C++ standard,
>
>class A
>{
>public:
>static const int m_a=1;
>};
>
>I've seen some people used such codes, however, it caused compling error
>
>in VC++5.0. I was rather confused.

This one is not really MS's fault as in class initialisation of static
const data (integer types only) was a late tweak to the Standard.  In
addition you still have to define the variable exactly once somewhere in
your program (for the linker)


Francis Glassborow      Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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: "Ruud Diterwich" <ruud@casema.net>
Date: 2000/04/29
Raw View
"Francis Glassborow" <francis@robinton.demon.co.uk> wrote in message
news:jRZNYBDqKCC5Ewpe@robinton.demon.co.uk...
> In article <3908CCBC.E02307C@hotmail.com>, Yaxun Liu
> <yx_liu@hotmail.com> writes
> >Hi,
> >  I wonder whether the following codes satisfy the C++ standard,
> >
> >class A
> >{
> >public:
> >static const int m_a=1;
> >};
> >
> >I've seen some people used such codes, however, it caused compling error
> >
> >in VC++5.0. I was rather confused.
>
> This one is not really MS's fault as in class initialisation of static
> const data (integer types only) was a late tweak to the Standard.  In
> addition you still have to define the variable exactly once somewhere in
> your program (for the linker)
>

Sometimes you need the const value in the header, for example to declare a
static array member. A way to do this is to use an enum to define the
constant:

class A
{
public:
    enum { m_a = 1 };
    char m_characters[m_a + 1];
};



---
[ 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              ]