-
Why did C++ make any distinction between static globals (internal linkage) and symbols in an unnamed namespace (external linkage, but no way to refer to it from outside anyway), when introducing the latter?
-
Are any of those reasons still valid, or are there new ones?
-
Are there any places left where they are still different but the arbitrary rule that anonymous global (or namespace-scope) unions must be
static
, and what are they? -
For bonus-points, if there are no good reasons left for them to be different, is there a request to make them equivalent?
When C++ introduced namespaces (C++98), and specifically unnamed namespaces, static globals were deprecated as obsolete and inferior to the new thing in a bout of enthusiasm, though that was reverted with C++11:
Deprecation of the static keyword… no more?
Before C++11, symbols with internal linkage could not be used as template-arguments: Why did C++03 require template parameters to have external linkage?
Best Answer
I don't suppose this answers all of your questions (or any of them?), but the key difference between file-level static declarations and anonymous namespaces is that the namespaces also apply to types (you can't declare a
static
type in the same sense you declare a variable), that's why the namespace is preferred, so there's a single idiom to declare file-scoped data and types.Exemplifying, the following code should compile just fine. (Not really useful, since you can't distinguish between both types, but allowed)
A live test here.