c++ - 指定不明确/全局命名空间的关键字?

标签 c++ namespaces global

在此代码片段中,找到 here .

void f() {
    d1++; // error: ambiguous ::d1 or D::d1?
    ::d1++; // OK <----**This one**
    D::d1++; // OK
    d2++; // OK, d2 is D::d2
    e++; // OK: e is E::e due to transitive using
    f(1); // error: ambiguous: D::f(int) or E::f(int)?
    f('a'); // OK: the only f(char) is D::f(char)
}

有一段代码用于指定全局命名空间( ::d1++; )。

我四处闲逛,很难给这个“分类”命名;并且正在寻找更好的选择。是否有任何关键字可以实现此功能,或者是否有任何更明确的替代方法,以便它不会被解释为错误?

我最好的猜测是将其称为非声明性命名空间。任何更正将不胜感激。

最佳答案

代码是普通的 C++ 代码,所以应该没有问题。
如果您仍想进行更改,您可以:

1)使用宏

#define global(x) ::x

2)使用别名auto& global_d1 = ::d1;更改名称以免发生碰撞

关于c++ - 指定不明确/全局命名空间的关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59203643/

相关文章:

JavaScript 无法访问文档 : null or undefined

c++ - Visual Studio 2013 dll 导出链接错误 (LNK2019/LNK1120)

c++ - 实现锁

c++ - 找到巨大的已分配内存块

visual-studio - 在 Visual Studio 中为新项目设置默认命名空间?

jQuery 使用命名空间解析 XML

xcode - 如何在 Swift 中创建全局变量?

c++ - boost 正则表达式构造函数中的互斥断言

python - 使用 python 缩进命名空间

python - Tkinter 全局绑定(bind)