3

Possible Duplicate:
Superiority of unnamed namespace over static?
Why unnamed namespace is a“ superior” alternative to static?

I know know anonymous namespaces are "encouraged" over static globals which are more C like (and deprecated), etc etc, and I use them often myself. However, despite having read other posts and questions about this topic, I haven't really seen explicit reasons why an anonymous namespace is better than a set of simple static globals.

Is there a definite reason why I should stick to the former?

Community
  • 1
  • 1
8bitcartridge
  • 1,629
  • 6
  • 25
  • 38
  • 1
    And see this as well : [Why unnamed namespace is a“ superior” alternative to static?](http://stackoverflow.com/questions/4977252/why-unnamed-namespace-is-a-superior-alternative-to-static) – Nawaz Nov 25 '11 at 05:10
  • Possible duplicate: http://stackoverflow.com/q/4977252/187543 – cpx Nov 25 '11 at 05:11
  • Also: http://stackoverflow.com/questions/154469/unnamed-anonymous-namespaces-vs-static-functions – Gnawme Nov 25 '11 at 05:11
  • Also: http://stackoverflow.com/q/8236755/187543 – cpx Nov 25 '11 at 05:12

1 Answers1

3

Static variables at namespace scope are no longer deprecated.

There's no particular reason to prefer one over the other. A minor reason to prefer an unnamed namespace is that you can declare anything inside it, while only functions and variables can be static. A minor reason to prefer static declarations is that you don't have extra braces loitering around the declaration. Use whichever feels more harmonious to you.

Historical note: before C++11, there was one reason to prefer a namespace: for some bizarre reason, pointers to static objects or functions could not be used as template arguments, while pointers to non-static ones could. C++11 removes this restriction, along with the equally odd restrictions preventing, for example, local types being template arguments.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644