This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: static const class member is undefined


"Erdi Chen" <erdi@chen11.com> writes:

> Perhaps I wasn't specific enough: I am talking about static const class
> member variables of integral types.  ISO C++ allows these variables to be
> initialized inside the class definition. Either way, it would be a bug
> since only very specific code would cause this error: "return n != 0 ?
> Foo::c : Foo::d;".  Other use of the variables, such as "return Foo::c;",
> is okay.  Gcc generates a literal for the latter but a variable reference
> for the former.

It probably is a bug that GCC generates a reference to the variable,
since GCC should optimise by replacing the variable by its constant
value, but the program is still ill-formed because ISO C++ does say:

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

> 
> >> G++ creates undefined symbols for static const class member variables
> >> for the test code in the attached file.
> >
> > It's ISO C++: class static data members must have a definition outside
> > the class definition, even if they are const.
> >
> > --
> > Eric Botcazou
> >
> 
> 

-- 
- Geoffrey Keating <geoffk@geoffk.org>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]