是否可以通过在调用者代码中使用 using 声明(或类似的东西)来访问Foo命名空间下的类型first?
namespace first {
namespace second {
struct Foo { int i; };
}
}
int main() {
using namespace first::second;
first::Foo foo { 123 };
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到这些错误消息:
error: 'Foo' is not a member of 'first'
first::Foo foo{ 123 };```
Run Code Online (Sandbox Code Playgroud)
您有多种选择:
using namespace first::second;
Foo foo{123};
Run Code Online (Sandbox Code Playgroud)
namespace ns = first::second;
ns::Foo foo{123};
Run Code Online (Sandbox Code Playgroud)
我想你也可以这样做namespace first = first::second;。然后first::Foo foo{123};会起作用,但要访问实际内容namespace first(除了 中的内容namespace second),您必须使用::first.