When I compile the following sources on VC++ 10, The i with static linkage gets assigned to 42
But on G++ 4.5.1, The i with external linkage in source2.cpp gets assigned to 42.
Any ideas on what should be the standard confirming behavior according to the Standard or why?
// source1.cpp
#include <iostream>
static int i = 0;
int h();
void foo()
{
int i;
{
extern int i;
i = 42;
}
}
int main()
{
foo();
std::cout << i << std::endl;
std::cout << h() << std::endl;
}
// source2.cpp
int i;
int h() { return i; }
ISO/IEC 14882:2011 3.5/6:
Inside the inner block in
foo(), The declarationint i;hides the declaration at global namespace scope:static int i;so there is no visibleiwith linkage inside the inner block. This means thatextern int i;refers to an entity with external linkage in the namespace immediately enclosingfoo().The assignment should affect the
iwith external linkage (defined insource2.cpp), it should have no effect on theiwith internal linkage defined insource1.cpp.