'使用命名空间{一些命名空间嵌套在另一个内部}`
'using namespace {some namespace nested inside of another}`
是否可以通过在调用者代码中使用 using 声明(或类似的东西)来访问命名空间 first
下的类型 Foo
?
namespace first {
namespace second {
struct Foo { int i; };
}
}
int main() {
using namespace first::second;
first::Foo foo { 123 };
return 0;
}
我收到这些错误消息:
error: 'Foo' is not a member of 'first'
first::Foo foo{ 123 };```
您有多种选择:
-
using namespace first::second;
Foo foo{123};
-
namespace ns = first::second;
ns::Foo foo{123};
我想你也可以 namespace first = first::second;
。然后 first::Foo foo{123};
就可以了,但是要访问实际 namespace first
的内容(namespace second
中的内容除外),您必须使用 ::first
.
命名空间可以扩展,您可以为不同命名空间中的令牌创建别名:
namespace first {
namespace second {
struct Foo { int i; };
}
}
namespace first {
using second::Foo;
}
int main() {
first::Foo foo { 123 };
return 0;
}
是否可以通过在调用者代码中使用 using 声明(或类似的东西)来访问命名空间 first
下的类型 Foo
?
namespace first {
namespace second {
struct Foo { int i; };
}
}
int main() {
using namespace first::second;
first::Foo foo { 123 };
return 0;
}
我收到这些错误消息:
error: 'Foo' is not a member of 'first'
first::Foo foo{ 123 };```
您有多种选择:
-
using namespace first::second; Foo foo{123};
-
namespace ns = first::second; ns::Foo foo{123};
我想你也可以
namespace first = first::second;
。然后first::Foo foo{123};
就可以了,但是要访问实际namespace first
的内容(namespace second
中的内容除外),您必须使用::first
.
命名空间可以扩展,您可以为不同命名空间中的令牌创建别名:
namespace first {
namespace second {
struct Foo { int i; };
}
}
namespace first {
using second::Foo;
}
int main() {
first::Foo foo { 123 };
return 0;
}