Bug 32056 - g++ does not detect non-integral in-class initializer
Summary: g++ does not detect non-integral in-class initializer
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc
Version: 7.0
Hardware: i386
OS: Linux
medium
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: David Lawrence
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2001-03-17 04:29 UTC by Wagner T. Correa
Modified: 2007-04-18 16:32 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2001-03-17 04:29:47 UTC
Embargoed:


Attachments (Terms of Use)

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 {
public:
	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'


Note You need to log in before you can comment on or make changes to this bug.