I was of the impression that C++ applies the same special rules to a static const integral type regardless of whether declared at namespace scope or declared within a class/struct/union.
Now I’m thinking that I’ve been taught Bad Things by non-compliant compilers.
static const int A = 1;
struct s
{
static const int A = 1;
};
Aside from the obvious difference in scope, How do A and s::A differ?
- …when their usage will be replaced with their literal value?
- …when I can take the address of it?
- …when I need to separately define them?
I’m curious specifically about C++ 03.
The keyword
staticdoesn’t mean the same thing in class scopeand in namespace scope. In fact, it’s use in namespace scope is
deprecated.
When declaring a variable at class scope,
staticmeans thatthere will be one single instance of the variable, with static
storage duration and lifetime. The declaration within the class
is not a definition; if the variable is used, it must be
defined in one (and only one) translation units; if it is not
defined, you have undefined behavior. (In practice, depending
on the use, either everything will work fine, or you will get an
error from the linker.) Note that if the declaration is for
a
constintegral type, and contains an initialization, it isnot considered used if it is used in a context which requires
a constant integral expression (like the dimension of a C style
array). The simplest and surest thing is just to define it
somewhere.
When declaring a variable at namespace scope,
staticmeansthat the name has internal linkage, rather than external; with
or without
static, the declaration is a definition (so thereshould be no other definition in the program). In C++03, this
use was deprecated; use unnamed namespace instead. Note too
that if the variable itself is
const(top level const), thenit has internal linkage by default, so the
statichas noeffect whatsoever. (If you need a
constvariable withexternal linkage, make it a class member, or define it
explicitly
extern, using an initializer to make ita definition, rather than just a declaration.)