The C++11 standard give the code snippet below (I deleted unrelated code) and said the name i
have external linkage. (clause 3.5.6)
static int i = 0; // #1
void g() {
extern int i; // #3 external linkage
}
Why do they do this? Did I misunderstand something? The two i
refer to the same object in vs2012. And when I use i
somewhere else, i got an unresolved external error. I have no idea whether vs2012 support this feature or not.
Edit:
I think VS2012 is doing the right thing. The i
in #3 only need to refers to an i
that has a linkage. If the compiler can't find one, than the i
should be defined in other translation unit. So the two i
should refer to the same object in the code snippet above.
The quote from the standard:
If there is a visible declaration of an entity with linkage having the same name and type, ignoring entities declared outside the innermost enclosing namespace scope, the block scope declaration declares that same entity and receives the linkage of the previous declaration. if no matching entity is found, the block scope entity receives external linkage.
But why people need this feature?