7

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?

yohjp
  • 2,985
  • 3
  • 30
  • 100
  • 1
    I think this is because in the `namespace-body` you access the entities without extra qualification, which requires that `using namespace unique` be in effect already. – Matthieu M. Nov 29 '12 at 10:00
  • 3
    @mat in the body of a namespace you can always refer to things defined in that namespace without qualification – Johannes Schaub - litb Nov 29 '12 at 10:11
  • @JohannesSchaub-litb: I know, I was wondering about the effect of clashes with the surrounding environment though – Matthieu M. Nov 29 '12 at 11:03

1 Answers1

7

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.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 3
    It took me a while to figure out from your answer that what you mean is what I would express as, "the definition in the standard ensures that names declared in the namespace-body can be referred to in the namespace-body by fully-qualifying them using the global namespace. For example ..." – Steve Jessop Nov 29 '12 at 10:38