匿名命名空间 [英] Anonymous Namespace

查看:111
本文介绍了匿名命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO上的最近线程触发了此操作。

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 }

我无法回忆它不等同于

  namespace unique { namespace-body } 
  using namespace unique;

也尝试搜索(包括google),但是徒劳。

Also tried searching (including google) but in vain. Please share any information you have in this regards.

推荐答案

现在存在的规范是在1995年在 N0783 修正角落情况。引用该论文(第9页):

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


WP将未命名的命名空间的语义定义为等同于:

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;
    }
}

应该更改WP以定义未命名的命名空间等价于:

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

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


这篇关于匿名命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆