Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ unnamed(anonymous) namespace definition

Tags:

c++

namespaces

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?

like image 430
yohjp Avatar asked Nov 29 '12 09:11

yohjp


1 Answers

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.

like image 101
Johannes Schaub - litb Avatar answered Sep 28 '22 11:09

Johannes Schaub - litb