Search code examples
c++namespaceslanguage-designunnamed-namespace

C++ unnamed(anonymous) namespace definition


C++03 Standard 7.3.1.1 [namespace.unnamed] paragraph 1: (and C++11 Standard also use similar definition)

An unnamed-namespace-definition behaves as if it were replaced by

namespace unique { /* empty body */ }
using namespace unique;
namespace unique { namespace-body }

Why not is it simply following definition?

namespace unique { namespace-body }
using namespace unique;

Side question: MSDN defines by latter form. Does it violate Standard technically?


Solution

  • You could not do this anymore

    namespace { typedef int a; ::a x; }
    

    Note that in a subsequent namespace { ... }, suddenly you could. This would be horribly inconsistent.

    Also notice this case, with two different valid outcomes

    namespace A { void f(long); }
    using namespace A;
    
    namespace { 
      void f(int);
      void g() {
        ::f(0);
      }
    }
    

    With ISO C++, this calls the int version of f. With your alternative definition, it calls the long version.