static const data member

W

wkaras

Section 9.4.2 paragraph 4 of the draft Standard says:

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 (expr.const)
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.

It sounds like the last sentance is saying that the definition in
"namespace" scope is still always necessary. Yet GCC compiles
and links this just fine:

#include "stdio.h"

class A { public: static const int I = 10; };

int main(void) { printf("%d\n", A::I); return(0); }

Assembler output shows GCC never reserves space in memory for I .

Is GCC non-compliant here for not pointlessly outputting an error
for the lack of a definition (of I) that would only cause a
waste of memory? Or was this changed in the final standard? Or,
am I just interpreting the standard wrong?
 
A

Alf P. Steinbach

* (e-mail address removed):
Section 9.4.2 paragraph 4 of the draft Standard says:

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 (expr.const)
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.

It sounds like the last sentance is saying that the definition in
"namespace" scope is still always necessary. Yet GCC compiles
and links this just fine:

#include "stdio.h"

class A { public: static const int I = 10; };

int main(void) { printf("%d\n", A::I); return(0); }

Assembler output shows GCC never reserves space in memory for I .

Is GCC non-compliant here for not pointlessly outputting an error
for the lack of a definition (of I) that would only cause a
waste of memory?

Technically yes, but IIRC the Boost library does the same as you do, and
works on a very wide range of compilers.

Or was this changed in the final standard?

Don't know.

Or, am I just interpreting the standard wrong?

I don't think so.
 
V

Victor Bazarov

Section 9.4.2 paragraph 4 of the draft Standard says:

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 (expr.const)
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.

It sounds like the last sentance is saying that the definition in
"namespace" scope is still always necessary. Yet GCC compiles
and links this just fine:

#include "stdio.h"

class A { public: static const int I = 10; };

int main(void) { printf("%d\n", A::I); return(0); }

Assembler output shows GCC never reserves space in memory for I .

Is GCC non-compliant here for not pointlessly outputting an error
for the lack of a definition (of I) that would only cause a
waste of memory? Or was this changed in the final standard? Or,
am I just interpreting the standard wrong?

See defect report 454. If the address is not taken, the data member may
not need to be defined. If it's only used as an rvalue, it is acceptable
to omit the definition. I guess G++ is ahead of the Standard Committee.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top