hamishmcn
hamishmcn

Reputation: 7981

C++ Namespace question

I am working on some code written by a co-worker who no longer works with the company, and I have found the following code: (which I have cut down below)

namespace NsA { namespace NsB { namespace NsC {

    namespace { 
        class A { /*etc*/ };
        class B { /*etc*/ };
    }    

    namespace {
        class C { /*etc*/ };
    }
} } }

I don't understand the purpose of the namespace commands on lines 3 and 8.
Can someone explain what the purpose of an namespace entry with no name is?
Thanks

Upvotes: 17

Views: 2926

Answers (1)

Joris Timmermans
Joris Timmermans

Reputation: 10988

That's an "anonymous namespace" - which creates a hidden namespace name that is guaranteed to be unique per "translation unit" (i.e. per CPP file).

This effectively means that all items inside that namespace are hidden from outside that compilation unit. They can only be used in that same file. See also this article on unnamed namespaces.

Upvotes: 37

Related Questions