Search code examples
c++namespaces

Nested or non-nested anonymous namespace to hide implementation details


What's the difference between nesting anonymous namespace vs having it at top level in the translation unit?

namespace Named {
    namespace {
        void impl () {...}
   }

   void fun ()
   {
       impl();
   }
}

Or

namespace {
    void impl () {...}
}

namespace Named {
    void fun ()
    {
        impl();
    }
 }

Solution

  • Same as with all namespaces - lookup rules. In first example, if Named already has own void impl(){}, that would be an error. In the second, if Named::impl exists, it will be silently taken over anonymous-namespace::impl. See it online