Anonymous Namespace

Could someone give an example of an anonymous namespace?

What context? In C++ they look like:

    namespace { // Look no name!
         void proc() {
              //...
         }
    }

The meaning is similar to:

    namespace __anonymous_49841092843120931 {
         void proc() {
              //...
         }
    }
    using namespace __anonymous_49841092843120931;

where the number is guaranteed unique to a translation unit. The effect is similar to declaring proc() "static" in C, but without actually giving it static linkage (which matters eg for templates).

I hope this helps. -- DaveHarris

By the way, compilers aren't supposed to make an number ID for the namespace because you may link against third party libraries and have a conflict. Instead, the resolution is done at link time through some magic. Some compilers do actually use a "unique identifier" and the 'net occasionally hears echoes of their customer's screams of frustration and horror.

As long as the number is unique for the translation unit it will be fine, a third party library will then get another number. The double underscore guarantees that the name is reserved for the implementation and third party libraries aren't supposed to use names reserved for someone else. GCC for example does not rely on link time magic.


CategoryCpp


EditText of this page (last edited February 7, 2007) or FindPage with title or text search