'使用命名空间{某些命名空间嵌套在另一个内部}`

4 c++ namespaces

是否可以通过在调用者代码中使用 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)

Hol*_*Cat 5

您有多种选择:

  1. using namespace first::second; 
    Foo foo{123};
    
    Run Code Online (Sandbox Code Playgroud)
  2. namespace ns = first::second; 
    ns::Foo foo{123};
    
    Run Code Online (Sandbox Code Playgroud)
  3. 我想你也可以这样做namespace first = first::second;。然后first::Foo foo{123};会起作用,但要访问实际内容namespace first(除了 中的内容namespace second),您必须使用::first.