Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Namespace question

Tags:

c++

namespaces

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

like image 977
hamishmcn Avatar asked Nov 25 '08 15:11

hamishmcn


1 Answers

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.

like image 82
Joris Timmermans Avatar answered Sep 23 '22 14:09

Joris Timmermans