This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: C++ static integer class constants...
- From: corey taylor <corey dot taylor at gmail dot com>
- To: John Ratliff <webmaster at technoplaza dot net>
- Cc: MSX to GCC <gcc-help at gcc dot gnu dot org>
- Date: Mon, 17 Oct 2005 13:07:11 -0500
- Subject: Re: C++ static integer class constants...
- References: <BF78FD38.333A%eljay@adobe.com> <4353e481.0829b6d6.64bd.18a4SMTPIN_ADDED@mx.gmail.com>
The 2003 specification basically agrees:
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. 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.
The explanation of "use" is obvious in that context as meaning used
outside of a constant integral expression.
corey
On 10/17/05, John Ratliff <webmaster@technoplaza.net> wrote:
> Well, this just gets more complicated everyday.
>
> The 9.4.2, par 4 section of the standard 1998 in any case (I don't have
> access to the 2003 version) says you must declare the variable. But then it
> goes on to say that if you do, you can't use the initializer. So g++ 3.3 is
> correct, and 3.4 is just a better optimizer.
>
> So, you can use named static constants as long as they only appear in the
> class-def, and I am supposed to define them if they are "used" in the
> program, though the definition of "used" is somewhat debatable.
>
> However, this is not what was intended, and is proposed for change.
>
> The DR on this:
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#48
>
> More on this:
> http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#454
>
> I already know bout the enum hack, but this is supposed to supplant and
> remove it.
>
> --John Ratliff
>
>
> > -----Original Message-----
> > From: John Love-Jensen [mailto:eljay@adobe.com]
> > Sent: Monday, October 17, 2005 7:03 AM
> > To: John Ratliff; MSX to GCC
> > Subject: Re: C++ static integer class constants...
> >
> > Hi John,
> >
> > To get the behavior you want without having to define the static class
> > constant in some translation unit, use an anonymous enum:
> >
> > class foo {
> > public:
> > enum { X = 5, Y = 10, Z = 15 };
> > };
> >
> > >Is this correct behavior?
> >
> > I believe your example demonstrates the correct behavior.
> >
> > In that *IF* you specify the value of a static const int in the
> > declaration, that both you *MUST* define the static const int in
> > one-and-only-one translation unit, and you *MUST NOT* specify the
> > initialization value in that definition.
> >
> > HTH,
> > --Eljay
> >
>
>
>
>