71

When would one use unnamed namespace in C++ ? Is it better in any sense than a free standing function? Also, should it be used only in source file and not in header file?

Asha
  • 11,002
  • 6
  • 44
  • 66
  • 2
    See this topic also : [Superiority of unnamed namespace over static ?](http://stackoverflow.com/questions/4422507/superiority-of-unnamed-namespace-over-static) – Nawaz Mar 15 '11 at 13:16

4 Answers4

64

According to Stroustrup, you should use it in places where in old C you would have made static globals. The idea is that the items in question can be "global" to the source file they are in, but not pollute the namespace of any other source files in your compilation.

In other words, you shouldn't be creating static globals in C++. You should be using unnamed namespaces instead.

I have found some situations where they are useful in header files, but that should be rare. Mostly I think for declaring throwable exceptions. In that case the definitions in question will be global for everything that #includes that header, but not for things that don't.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
  • 3
    `static` is no longer marked as deprecated in the latest issue of the FCD (n3225). The committee recognized that it's less verbose for a single function or global. It is also easier to "grep", since it appears near the declaration, and does not require a parser. – Matthieu M. Mar 15 '11 at 13:29
  • 6
    @Matthieu: but then the functions have internal linkage. The real reason that unnamed namespaces are called "superior" is that they "hide" the symbol rather than changing the object/function's linkage, which is a closer match to what people usually want to achieve when they use it. That hasn't changed in C++0x, they've recognised that (1) if you fear that external linkage will be expensive, then you want internal linkage, which in turn means you want `static`, and (2) they're never going to remove `static` in this context because of C compatibility, so deprecating it is an empty threat. – Steve Jessop Mar 15 '11 at 15:06
  • 3
    @Steve: I was expecting it to also change the linkage, what's the goal of maintaining it external if no-one knows the symbol ? – Matthieu M. Mar 15 '11 at 15:19
  • @Matthieu: in C++03, to make it legal to use it as a template argument. I think C++0x makes it legal to use internal-linkage stuff in templates, I could be wrong. I've sort of forgotten whether we're supposed to be talking about functions or objects, though... – Steve Jessop Mar 15 '11 at 15:33
  • @Steve: Ah I see, I remember there was a number of restrictions in C++0x (the impossibility to use function-local classes as predicates for template code being one) which might be due to linkage issues. Thanks :) – Matthieu M. Mar 15 '11 at 15:49
  • 3
    @SteveJessop: _if you fear that external linkage will be expensive, then you want internal linkage, which in turn means you want static_ With C++11 (§3.5/4) unnamed namespaces seem to have internal linkage like `static`. – legends2k Oct 21 '13 at 18:25
29

Unnamed namespace is private to the translation unit and this can be used to shield global variables and functions with same names occurring in different translation units so that no link conflicts arise.

For example, you need a class that will only be defined in a .cpp file and used only within that file. You want to call it CModuleLock. If it is not in an unnamed namespace and some other .cpp file accidentially has another class CModuleLock not in an unnamed namespace you won't be able to link your program.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 1
    I sometimes use unnamed namespaces for classes that could be private inner class, but i would like to share between similar classes (perhaps those implementing the same concept). Of course if the class becomes general enough i will move it to a named namespace – RichardBruce May 15 '13 at 14:54
11

It's used for name hiding. Each unnamed namespace is unique. The link here explains in more detail. It is typically used in a source file to hide functions that should only have internal linkage (e.g. not exposed to the outside world).

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
1

Unnamed namespaces are the "C++ version" of global static variables and functions. Note that you can also use a unnamed namespace for classes.

fbafelipe
  • 4,862
  • 2
  • 25
  • 40