By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,125 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,125 IT Pros & Developers. It's quick & easy.

static const data member

P: n/a
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?

Aug 2 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* wk****@yahoo.com:
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.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 2 '05 #2

P: n/a
wk****@yahoo.com wrote:
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
Aug 2 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.