gpt4 book ai didi

c++ - 匿名命名空间

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:57:56 26 4
gpt4 key购买 nike

A recent thread在 SO 上触发了这个。

匿名命名空间被认为等同于

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

我不记得为什么它不等同于的确切原因

  namespace unique { namespace-body } 
using namespace unique;

也尝试搜索(包括谷歌)但徒劳无功。请分享您在这方面的任何信息。

最佳答案

现在存在的规范是 1995 年在 N0783 中引入的纠正一个角落的情况。引用那篇论文(第 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
}

关于c++ - 匿名命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3673617/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com