Why an unnamed namespace is a “superior” alternative to static? [duplicate]

  • As you’ve mentioned, namespace works for anything, not just for functions and objects.
  • As Greg has pointed out, static means too many things already.
  • Namespaces provide a uniform and consistent way of controlling visibility at the global scope. You don’t have to use different tools for the same thing.
  • When using an anonymous namespace, the function/object name will get mangled properly, which allows you to see something like “(anonymous namespace)::xyz” in the symbol table after de-mangling, and not just “xyz” with static linkage.
  • As pointed out in the comments below, it isn’t allowed to use static things as template arguments, while with anonymous namespaces it’s fine.
  • More? Probably, but I can’t think of anything else right now.

Leave a Comment