1. 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?

  2. Are any of those reasons still valid, or are there new ones?

  3. 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?

  4. 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?


1 Answer 1


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)

#include <iostream>

struct Foobar
    int   foo;
    float bar;


struct Foobar
    double baz;

} // namespace

int main()
    std::cout << "HELLO!\n";

A live test here.

  • It compiles because you don't use Foobar in the main function.
    – dshil
    Dec 6, 2018 at 4:44
  • More importantly what happens if another .cpp file declares its own version of struct Foobar? Worse still, suppose it's now class Foobar. Think about how you plan to create constructors for both of them.
    – dgnuff
    May 23, 2019 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.