Apparently from §3.3.1/4, this snippet doesn't compile because it contains two different entities with the same name A
in the global namespace, extern int A;
and static int A = 101;
. That is, one has external and the other has internal linkage.
#include <iostream>
extern int A;
static int A = 101;
class A{};
int main()
{
std::cout << A << '\n';
}
Why then, does this code compile?
#include <iostream>
static int A = 101;
extern int A;
class A{};
int main()
{
std::cout << A << '\n';
}
Edit
I think the accepted answer for the question, of which this one is considered a dup, basically says that in the second snippet, variable A
still has internal linkage, despite the extern
declaration. But this is in disagreement with paragraph §3.5/4 that I mentioned below in a comment to @dyp.
§3.5/4:
An unnamed namespace or a namespace declared directly or indirectly within an unnamed namespace has internal linkage. All other namespaces have external linkage. A name having namespace scope that has not been given internal linkage above has the same linkage as the enclosing namespace if it is the name of
— a variable; or
...
Edit 1:
The OP uses §3.5/6 to justify his answer to the other question.
§3.5/6 (emphasis mine):
The name of a function declared in block scope and the name of a variable declared by a block scope extern declaration have linkage. 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 there is more than one such matching entity, the program is ill-formed. Otherwise, if no matching entity is found, the block scope entity receives external linkage.
It's clear this answer doesn't apply to the snippets shown on my question, as the declarations of the variable A
are not block scope declarations.
Edit 2:
This issue with "ready" status says that §7.1.1/7 should be deleted because it's false.