给出示例代码段:
namespace project_namespace::my_math
{
namespace enclosing_namespace = my_math;
struct sphere {};
struct sphere_stuff {
using sphere = enclosing_namespace::sphere;
};
struct line {};
struct line_stuff {
using line = enclosing_namespace::line;
} ;
}
int main()
{
using sphere_stuff = project_namespace::my_math::sphere_stuff;
sphere_stuff::sphere sphere;
using line_stuff = project_namespace::my_math::line_stuff;
line_stuff::line line;
return 0;
}
当我看到上面的代码时,我心里想:
所以我的问题有两个部分:
当 clang 和 g++ 编译器遇到这个“命名空间 enclusion_namespace = my_math”时,它们如何处理这个问题(c++17 和 c++20)?在上面的片段中(如果在大型代码库中使用),我是否创建了一种将命名空间复制到命名空间到命名空间等的情况,这对于编译器来说是一场噩梦?或者这对于编译器来说是微不足道的吗?
有什么理由不应该这样做(在 2023 年,c++17 或 c++20)吗?
他们为我提供了一个在线编译器的链接,该编译器生成 AST 转储,并且还澄清了基本上每个别名只插入一个节点。
我提供给他们的例子是:
namespace project {
namespace enclosing_namespace = project;
namespace namespace_1 {
namespace parent_namespace = project;
namespace enclosing_namespace = namespace_1;
namespace b {
namespace parent_namespace = namespace_1;
namespace enclosing_namespace = b;
struct X {};
struct Y {
using Z = enclosing_namespace::X;
} ;
}
}
namespace namespace_2 {
namespace parent_namespace = project;
namespace enclosing_namespace = namespace_2;
namespace a {
namespace parent_namespace = namespace_2;
namespace enclosing_namespace = a;
struct X {};
struct Y {
using Z = enclosing_namespace::X;
} ;
}
}
}
int main()
{
project::namespace_1::parent_namespace::namespace_2::a::Y::Z z;
return 0;
}
转储看起来像这样:
TranslationUnitDecl
|-NamespaceDecl <line:1:1, line:33:1> line:1:11 project
| |-NamespaceAliasDecl <line:2:5, col:37> col:15 enclosing_namespace
| | `-Namespace 'project'
| |-NamespaceDecl <line:4:5, line:17:5> line:4:15 namespace_1
| | |-NamespaceAliasDecl <line:5:9, col:38> col:19 parent_namespace
| | | `-Namespace 'project'
| | |-NamespaceAliasDecl <line:6:9, col:41> col:19 enclosing_namespace
| | | `-Namespace 'namespace_1'
| | `-NamespaceDecl <line:8:9, line:16:9> line:8:19 b
| | |-NamespaceAliasDecl <line:9:13, col:42> col:23 parent_namespace
| | | `-Namespace 'namespace_1'
| | |-NamespaceAliasDecl <line:10:13, col:45> col:23 enclosing_namespace
| | | `-Namespace 'b'
| | |-CXXRecordDecl <line:12:13, col:23> col:20 referenced struct X definition
| | | |-DefinitionData pass_in_registers empty aggregate standard_layout trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
| | | | |-DefaultConstructor exists trivial constexpr needs_implicit defaulted_is_constexpr
| | | | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
| | | | |-MoveConstructor exists simple trivial needs_implicit
| | | | |-CopyAssignment simple trivial has_const_param needs_implicit implicit_has_const_param
| | | | |-MoveAssignment exists simple trivial needs_implicit
| | | | `-Destructor simple irrelevant trivial needs_implicit
| | | `-CXXRecordDecl <col:13, col:20> col:20 implicit struct X
| | `-CXXRecordDecl <line:13:13, line:15:13> line:13:20 struct Y definition
| | |-DefinitionData pass_in_registers empty aggregate standard_layout trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
| | | |-DefaultConstructor exists trivial constexpr needs_implicit defaulted_is_constexpr
| | | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
| | | |-MoveConstructor exists simple trivial needs_implicit
| | | |-CopyAssignment simple trivial has_const_param needs_implicit implicit_has_const_param
| | | |-MoveAssignment exists simple trivial needs_implicit
| | | `-Destructor simple irrelevant trivial needs_implicit
| | |-CXXRecordDecl <col:13, col:20> col:20 implicit struct Y
| | `-TypeAliasDecl <line:14:17, col:48> col:23 Z 'enclosing_namespace::X':'project::namespace_1::b::X'
| | `-ElaboratedType 'enclosing_namespace::X' sugar
| | `-RecordType 'project::namespace_1::b::X'
| | `-CXXRecord 'X'
| `-NamespaceDecl <line:19:5, line:32:5> line:19:15 namespace_2
| |-NamespaceAliasDecl <line:20:9, col:38> col:19 parent_namespace
| | `-Namespace 'project'
| |-NamespaceAliasDecl <line:21:9, col:41> col:19 enclosing_namespace
| | `-Namespace 'namespace_2'
| `-NamespaceDecl <line:23:9, line:31:9> line:23:19 a
| |-NamespaceAliasDecl <line:24:13, col:42> col:23 parent_namespace
| | `-Namespace 'namespace_2'
| |-NamespaceAliasDecl <line:25:13, col:45> col:23 enclosing_namespace
| | `-Namespace 'a'
| |-CXXRecordDecl <line:27:13, col:23> col:20 referenced struct X definition
| | |-DefinitionData pass_in_registers empty aggregate standard_layout trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
| | | |-DefaultConstructor exists trivial constexpr defaulted_is_constexpr
| | | |-CopyConstructor simple trivial has_const_param implicit_has_const_param
| | | |-MoveConstructor exists simple trivial
| | | |-CopyAssignment simple trivial has_const_param needs_implicit implicit_has_const_param
| | | |-MoveAssignment exists simple trivial needs_implicit
| | | `-Destructor simple irrelevant trivial needs_implicit
| | |-CXXRecordDecl <col:13, col:20> col:20 implicit struct X
| | |-CXXConstructorDecl <col:20> col:20 implicit used constexpr X 'void () noexcept' inline default trivial
| | | `-CompoundStmt <col:20>
| | |-CXXConstructorDecl <col:20> col:20 implicit constexpr X 'void (const X &)' inline default trivial noexcept-unevaluated 0xbb9e218
| | | `-ParmVarDecl <col:20> col:20 'const X &'
| | `-CXXConstructorDecl <col:20> col:20 implicit constexpr X 'void (X &&)' inline default trivial noexcept-unevaluated 0xbb9e418
| | `-ParmVarDecl <col:20> col:20 'X &&'
| `-CXXRecordDecl <line:28:13, line:30:13> line:28:20 struct Y definition
| |-DefinitionData pass_in_registers empty aggregate standard_layout trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
| | |-DefaultConstructor exists trivial constexpr needs_implicit defaulted_is_constexpr
| | |-CopyConstructor simple trivial has_const_param needs_implicit implicit_has_const_param
| | |-MoveConstructor exists simple trivial needs_implicit
| | |-CopyAssignment simple trivial has_const_param needs_implicit implicit_has_const_param
| | |-MoveAssignment exists simple trivial needs_implicit
| | `-Destructor simple irrelevant trivial needs_implicit
| |-CXXRecordDecl <col:13, col:20> col:20 implicit struct Y
| `-TypeAliasDecl <line:29:17, col:48> col:23 referenced Z 'enclosing_namespace::X':'project::namespace_2::a::X'
| `-ElaboratedType 'enclosing_namespace::X' sugar
| `-RecordType 'project::namespace_2::a::X'
| `-CXXRecord 'X'
`-FunctionDecl <line:35:1, line:40:1> line:35:5 main 'int ()'
`-CompoundStmt <line:36:1, line:40:1>
|-DeclStmt <line:37:5, col:67>
| `-VarDecl <col:5, col:66> col:66 z 'project::namespace_1::parent_namespace::namespace_2::a::Y::Z':'project::namespace_2::a::X' callinit
| `-CXXConstructExpr <col:66> 'project::namespace_1::parent_namespace::namespace_2::a::Y::Z':'project::namespace_2::a::X' 'void () noexcept'
`-ReturnStmt <line:39:5, col:12>
`-IntegerLiteral <col:12> 'int' 0
您还可以在这里使用在线编译器:https://godbolt.org/z/G4beW7YnP