Anonymous Namespace

China☆狼群 提交于 2019-12-12 12:16:39

问题


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 have in this regards.


回答1:


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
}


来源:https://stackoverflow.com/questions/3673617/anonymous-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!