c++ - 如何隐藏 `A::x` 并仅在这种情况下公开 `B::x`?

标签 c++ c++11 namespaces standards name-lookup

namespace A
{
int a = 1;
int x = 2;
}

namespace B
{
int b = 3;
int x = 4;
}

using namespace A;
using namespace B;
using B::x;

int main()
{   
    return x; // error : reference to 'x' is ambiguous
}

如何仅在这种情况下隐藏 A::x 并公开 B::x

最佳答案

你不能。

您将两个名称都纳入范围,仅此而已。

要解决这个问题,请不要那样做;避免使用命名空间

关于c++ - 如何隐藏 `A::x` 并仅在这种情况下公开 `B::x`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42832866/

相关文章:

c++ - 模板化隐式类型转换运算符

c++ - 避免 dynamic_cast 的模式

python - Python 中特定于操作系统的命名空间规则

c++ - 命名构造函数习语和继承

c++ - 链表中高效的 "Insert"函数

c++ - 将 const 成员函数转换为非常量

c++ - 使用用户定义的转换运算符的函数模板重载决议

ruby-on-rails-4 - 为什么 Rails 引擎的 gem 名称必须为 "match"模块/命名空间名称?

java - namespace 和 JDOM

c++ - 从编译时已知的日历日期创建 `std::chrono::time_point`