Bug 32056

Summary: g++ does not detect non-integral in-class initializer
Product: [Retired] Red Hat Linux Reporter: Wagner T. Correa <wtcorrea>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: David Lawrence <dkl>
Severity: medium Docs Contact:
Priority: medium    
Version: 7.0CC: wtcorrea
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2001-03-17 04:29:47 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Description Wagner T. Correa 2001-03-17 04:29:44 UTC
From Bugzilla Helper:
User-Agent: Mozilla/4.76 [en] (X11; U; Linux 2.2.16-22smp i686)

g++ fails to detect an illegal attempt to initialize a non-integral static
const member of a class inside the class declaration.

Reproducible: Always
Steps to Reproduce:
The following piece of code should generate an error:

#include <stdio.h>

class A {
	const static int N = 10; // OK
	const static float EPS = 0.0001; // illegal

const int A::N;
const float A::EPS;

int main()
	const int *pi = &A::N;
	printf("%d %d\n", A::N, *pi);

	const float *pf = &A::EPS;
	printf("%g %g\n", A::EPS, *pf);

	return 0;


Actual Results:  Currently, g++ (gcc-2.96-69) will accept that piece of
code, and produce an executable.

Expected Results:  g++ should abort compilation with an error.

See page 249 of  Bjarne Stroustrup, "The C++ Programming Language", Special
Edition, 2000.

Comment 1 Jakub Jelinek 2001-03-19 11:29:10 UTC
No, it does detect it, but handles this as an extension to the standard.
If you want a warning, simply run g++ with -pedantic, if you want this to be
an error, run with -pedantic-errors:
g++ -pedantic-errors test.C
test.C:6: ISO C++ forbids initialization of member constant `EPS' of
non-integral type `const float'