The following code doesn't emit an error:
class Curious {
public:
static const float c5 = 7.0;
};
The above code is illegal according to the following references. The compiler
does not emit an error (instead the error is caught at link-time if the member
constant is not defined).
According to paragraph 9.4.2.4 of the standard:
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.9). [...] The member shall still be
defined in a namespace scope if it used in the program and the namespace scope
definition shall not contain an initializer.
See also paragraph 10.4.6.2 of Stroustrup's "The C++ Programming Language":
It is also possible to initialize a static integral constant member by adding a
constant-expression initializer to its member declaration. For example:
class Curious {
public:
[...]
static const float c5 = 7.0; // error: in-class not integral
[...]
It's a good idea to flag this error to avoid errors that are more difficult to
catch at link-time. Apparently GCC does not require integral member constants to
be defined, but it does require other member constants to be defined somewhere.
Why not help enforce this at compile-time?
--
Summary: declaration/initialization of non-integral member
constants: compiler does not emit error
Product: gcc
Version: 4.0.0
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: papadopo at shfj dot cea dot fr
CC: gcc-bugs at gcc dot gnu dot org
GCC host triplet: sparc-sun-solaris2.8
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=21499