c++ namespaces oop

Why are unnamed namespaces used and what are their benefits?


I just joined a new C++ software project and I’m trying to understand the design. The project makes frequent use of unnamed namespaces. For example, something like this may occur in a class definition file:

namespace {
  const int SIZE_OF_ARRAY_X;
  const int SIZE_OF_ARRAY_Y;
  bool getState(userType*,otherUserType*);

newusertype::newusertype(...) {...

What are the design considerations that might cause one to use an unnamed namespace? What are the advantages and disadvantages?



    Unnamed namespaces are a utility to make an identifier translation unit local. They behave as if you would choose a unique name per translation unit for a namespace:

    namespace unique { /* empty */ }
    using namespace unique;
    namespace unique { /* namespace body. stuff in here */ }

    The extra step using the empty body is important, so you can already refer within the namespace body to identifiers like ::name that are defined in that namespace, since the using directive already took place.

    This means you can have free functions called (for example) help that can exist in multiple translation units, and they won’t clash at link time. The effect is almost identical to using the static keyword used in C which you can put in in the declaration of identifiers. Unnamed namespaces are a superior alternative, being able to even make a type translation unit local.

    namespace { int a1; }
    static int a2;

    Both a‘s are translation unit local and won’t clash at link time. But the difference is that the a1 in the anonymous namespace gets a unique name.

    Read the excellent article at comeau-computing Why is an unnamed namespace used instead of static? ( mirror).


    • 1

      You explain the relation to static. Can you please also compare with __attribute__ ((visibility ("hidden")))?

      – phinz

      Mar 22, 2020 at 15:16


    Having something in an anonymous namespace means it’s local to this translation unit (.cpp file and all its includes) this means that if another symbol with the same name is defined elsewhere there will not be a violation of the One Definition Rule (ODR).

    This is the same as the C way of having a static global variable or static function but it can be used for class definitions as well (and should be used rather than static in C++).

    All anonymous namespaces in the same file are treated as the same namespace and all anonymous namespaces in different files are distinct. An anonymous namespace is the equivalent of:

    namespace __unique_compiler_generated_identifer0x42 {
    using namespace __unique_compiler_generated_identifer0x42;



      Unnamed namespace limits access of class,variable,function and objects to the file in which it is defined. Unnamed namespace functionality is similar to static keyword in C/C++.
      static keyword limits access of global variable and function to the file in which they are defined.
      There is difference between unnamed namespace and static keyword because of which unnamed namespace has advantage over static. static keyword can be used with variable, function and objects but not with user defined class.
      For example:

      static int x;  // Correct 


      static class xyz {/*Body of class*/} //Wrong
      static structure {/*Body of structure*/} //Wrong

      But same can be possible with unnamed namespace.
      For example,

       namespace {
                 class xyz {/*Body of class*/}
                 static structure {/*Body of structure*/}
        } //Correct


      • 4

        What is static structure? Also why does it have to be static?

        – smac89

        Nov 27, 2020 at 5:27

      • 2

        @smac89 They probably meant static struct structure.

        Jan 5 at 19:48