tags:

views:

6934

answers:

7

A little-used feature of C++ is the ability to create unnamed (anonymous) namespaces, like so:

namespace {
    int cannotAccessOutsideThisFile() { ... }
} // namespace

You would think that such a feature would be useless -- since you can't specify the name of the namespace, it's impossible to access anything within it from outside. But these unnamed namespaces are accessible within the file they're created in, as if you had an implicit using-clause to them.

My question is, why or when would this be preferable to using static functions? Or are they essentially two ways of doing the exact same thing?

+46  A: 

The C++ Standard reads in section 7.3.1.1 Unnamed namespaces, paragraph 2:

The use of the static keyword is deprecated when declaring objects in a namespace scope, the unnamed-namespace provides a superior alternative.

Static only applies to names of objects, functions, and anonymous unions, not to type declarations.

luke
Head Geek asks about static keyword used against functions only.The static keyword applied to entity declared in namespace scope specifies its internal linkage.Entity declared in anonymous namespace has external linkage (C++/3.5) however it is guaranteed to live in uniquely named scope. This anonymity of unnamed namespace effectively hides its declaration making it accessible only from within a translation unit.The latter effectively works in the same manner as the static keyword.
mloskot
+1  A: 

Having learned of this feature only just now while reading your question, I can only speculate. This seems to provide several advantages over a file-level static variable:

  • Anonymous namespaces can be nested within one another, providing multiple levels of protection from which symbols can not escape.
  • Several anonymous namespaces could be placed in the same source file, creating in effect different static-level scopes within the same file.

I'd be interested in learning if anyone has used anonymous namespaces in real code.

Commodore Jaeger
Good speculations, but wrong. The scope of these namespaces is file-wide.
Konrad Rudolph
Not exactly true, if you define an anonymous namespace inside another namespace it is still only file wide, and can only be seen as being within that namespace. Try it.
Greg Rogers
I could be wrong but, I guess that no, it is not file-wide: It is accessible only to the code *after* the anonymous namespace. This is a subtle thingy, and usually, I would not want to pollute a source with multiple anonymous namespaces... Still, this can have uses.
paercebal
+4  A: 

Use of static keyword for that purpose is deprecated by the C++98 standard. The problem with static is that it doesn't apply to type definition. It's also an overloaded keyword used in different ways in different contexts, so unnamed namespaces simplify things a bit.

Firas Assaad
+6  A: 

Putting methods in an anonymous namespace prevents you from accidentally violating the One Definition Rule, allowing you to never worry about naming your helper methods the same as some other method you may link in.

And, as pointed out by luke, anonymous namespaces are preferred by the standard over static members.

hazzen
I was referring to static stand-alone functions (i.e. file-scoped functions), not static member functions. Static stand-alone functions are much the same as functions in an unnamed namespace, thus the question.
Head Geek
Ah; well, the ODR still applies. Edited to remove paragraph.
hazzen
+4  A: 

I recently began replacing static keywords with anonymous namespaces in my code but immediately ran into a problem where the variables in the namespace were no longer available for inspection in my debugger. I was using VC60, so I don't know if that is a non-issue with other debuggers. My workaround was to define a 'module' namespace, where I gave it the name of my cpp file.

For example, in my XmlUtil.cpp file, I define a namespace XmlUtil_I { ... } for all of my module variables and functions. That way I can apply the XmlUtil_I:: qualification in the debugger to access the variables. In this case, the '_I' distinguishes it from a public namespace such as XmlUtil that I may want to use elsewhere.

I suppose a potential disadvantage of this approach compared to a truly anonymous one is that someone could violate the desired static scope by using the namespace qualifier in other modules. I don't know if that is a major concern though.

I've done this too, but with `#if DEBUG namespace BlahBlah_private { #else namespace { #endif`, so the "module namespace" is only present in debug builds and true anonymous namespace is used otherwise. It would be nice if debuggers gave a nice way to handle this. Doxygen gets confused by it also.
Kristopher Johnson
+4  A: 

There is one edge case where static has a surprising affect (at least it was to me). The C++03 Standard states in 14.6.4.2/1:

For a function call that depends on a template parameter, if the function name is an unqualified-id but not a template-id, the candidate functions are found using the usual lookup rules (3.4.1, 3.4.2) except that:

  • For the part of the lookup using unqualified name lookup (3.4.1), only function declarations with external linkage from the template definition context are found.
  • For the part of the lookup using associated namespaces (3.4.2), only function declarations with external linkage found in either the tmeplate definition context or the template instantiation context are found.

...

The below code will call foo(void*) and not foo(S const &) as you might expect.

template <typename T>
int b1 (T const & t)
{
  foo(t);
}

namespace NS
{
  namespace
  {
    struct S
    {
    public:
      operator void * () const;
    };

    void foo (void*);
    static void foo (S const &);   // Not considered 14.6.4.2(b1)
  }

}

void b2()
{
  NS::S s;
  b1 (s);
}

In itself this is probably not that big a deal, but it does highlight that for a fully compliant C++ compiler (ie. one with support for export) the static keyword will still have functionality that is not available in any other way.

// bar.h
export template <typename T>
int b1 (T const & t);

// bar.cc
#include "bar.h"
template <typename T>
int b1 (T const & t)
{
  foo(t);
}

// foo.cc
#include "bar.h"
namespace NS
{
  namespace
  {
    struct S
    {
    };

    void foo (S const & s);  // Will be found by different TU 'bar.cc'
  }
}

void b2()
{
  NS::S s;
  b1 (s);
}

The only way to ensure that the function in our unnamed namespace will not be found in templates using ADL is to make it static.

Richard Corden
Isn't the export keyword supposed to be cold dead? The only compilers supporting "export" are experimental ones, and unless surprises, "export" won't even be implemented in others because of unexpected side effects (in addition to not doing was it was expected for)
paercebal
See Herb Sutter's article on the subjet: http://www.gotw.ca/publications/mill23-x.htm
paercebal
The front-end from Edison Design Group (EDG) is anything but experimental. It is almost certainly the most standard conforming C++ implementation in the world. The Intel C++ compiler uses EDG.
Richard Corden
What C++ feature doesn't have 'unexpected side-effects'? In the case of export, it is that an unnamed namespace function will be found from a different TU - that is the same as if you included the template definition directly. It would be more surprising if it was not like this!
Richard Corden
+3  A: 

From experience I'll just note that while it is the C++ way to put formerly-static functions into the anonymous namespace, older compilers can sometimes have problems with this. I currently work with a few compilers for our target platforms, and the more modern Linux compiler is fine with placing functions into the anonymous namespace.

But an older compiler running on Solaris, which we are wed to until an unspecified future release, will sometimes accept it, and other times flag it as an error. The error is not what worries me, it's what it might be doing when it accepts it. So until we go modern across the board, we are still using static (usually class-scoped) functions where we'd prefer the anonymous namespace.

Don Wakefield