static const integral members
I have a question regarding the following snippet of code.
struct foo
{
static const int value = 1;
};
int main()
{
int bar = foo::value;
}
Is it valid C++?
First of all, I'm sure that the following is valid:
// foo is the same
int main()
{
int bar[foo::value];
}
And this one is not valid:
// foo is the same
int main()
{
const int& bar = foo::value;
}
But what about the first program in my post? Several respectful
C++ gurus (including Sutter and Straustrup) say that you can
use foo::value without definition if you don't take its address
and don't bind reference to it. But can it be confirmed by the
Standard?
9.4.2 Static data members [class.static.data]
4
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.19). In that
case, the member can appear in integral constant expressions. The
member shall still be defined in a name-
space scope if it is used in the program and the namespace scope
definition shall not contain an initializer.
3.2 One definition rule [basic.def.odr]
2
An expression is potentially evaluated unless it appears where an
integral constant expression is required
(see 5.19), is the operand of the sizeof operator (5.3.3), or is the
operand of the typeid operator and
the expression does not designate an lvalue of polymorphic class type
(5.2.8). An object or non-overloaded
function is **used** if its name appears in a potentially-evaluated
expression.
Those are not enough to answer the question, hence I ask you for help.
Roman Perepelitsa.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]