开发者

Anonymous Namespace

A recent thread on SO triggerred this.

An anonymous namespace is considered to be equivalent to

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

I fail to recollect the exact reason as to why it is not equivalent to

  namespace unique { namespace-body } 
  using namespace unique;

Also tried searching (including google) but in vain. Please share any information you ha开发者_StackOverflow中文版ve in this regards.


The specification that exists now was introduced in 1995 in N0783 to correct for a corner case. To quote that paper (page 9):

The WP defines the semantics of an unnamed namespace as being equivalent to:

namespace UNIQUE {
    // namespace body
}
using namespace UNIQUE;

This is incorrect because it makes the code in an unnamed namespace dependent on whether the code is in an original namespace or a namespace extension.

namespace {} // If you remove this line, the
             // use of ::f below is invalid

namespace {
    void f()
    {
        using ::f;
    }
}

The WP should be changed to define an unnamed namespace as being equivalent to:

namespace UNIQUE {}
using namespace UNIQUE;
namespace UNIQUE {
    // namespace body
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜