That line of code can actually appear in several different contexts and alghough it behaves approximately the same, there are small differences.
Namespace Scope
// foo.h
static const int i = 0;
'i
' will be visible in every translation unit that includes the header. However, unless you actually use the address of the object (for example. '&i
'), I'm pretty sure that the compiler will treat 'i
' simply as a type safe 0
. Where two more more translation units take the '&i
' then the address will be different for each translation unit.
// foo.cc
static const int i = 0;
'i
' has internal linkage, and so cannot be referred to from outside of this translation unit. However, again unless you use its address it will most likely be treated as a type-safe 0
.
One thing worth pointing out, is that the following declaration:
const int i1 = 0;
is exactly the same as static const int i = 0
. A variable in a namespace declared with const
and not explicitly declared with extern
is implicitly static. If you think about this, it was the intention of the C++ committee to allow const
variables to be declared in header files without always needing the static
keyword to avoid breaking the ODR.
Class Scope
class A {
public:
static const int i = 0;
};
In the above example, the standard explicitly specifies that 'i
' does not need to be defined if its address is not required. In other words if you only use 'i
' as a type-safe 0 then the compiler will not define it. One difference between the class and namespace versions is that the address of 'i
' (if used in two ore more translation units) will be the same for the class member. Where the address is used, you must have a definition for it:
// a.h
class A {
public:
static const int i = 0;
};
// a.cc
#include "a.h"
const int A::i; // Definition so that we can take the address