当前C++草案[namespace.def]/2中`that namespace`指代哪个命名空间?
that namespace in [namespace.def]/2 Alright, let's break this down clearly. The expression that namespace in [namespace.def]/2 refers specifically to the pre-existing, previously declared namespace that matches the identifier used in your named-namespace-definition, as identified by the lookup logic laid out in the clause.
First, let's restate the clause for reference:
[namespace.def]/2:在命名命名空间定义(named-namespace-definition)中,标识符为该命名空间的名称。若查找该标识符时,其指代的是在命名命名空间定义所在的命名空间中引入的命名空间名(而非命名空间别名namespace-alias),或是在该命名空间的内联命名空间集合(inline namespace set)的成员中引入的命名空间名,则该命名空间定义将扩展先前声明的命名空间。否则,该标识符将作为命名空间名被引入到命名命名空间定义所在的声明区域中。
Key Breakdown with Examples
Let's use concrete code examples to make this tangible:
Basic namespace extension case
// First declaration of namespace A in the global scope namespace A { int x = 1; } // This is a named-namespace-definition using identifier "A" namespace A { // The lookup for "A" finds the pre-declared global namespace A // So `that namespace` here refers to the original global namespace A int y = 2; }Here, the second
namespace Adefinition extends the originalA—that namespaceis exactly that original, pre-existingAnamespace.Inline namespace set case
namespace Library { inline namespace v2 { void func() {} } } // This definition targets the inline namespace v2 inside Library namespace Library::v2 { // Lookup for "v2" finds it as a member of Library's inline namespace set // `that namespace` here refers to Library::v2 void func(int) {} }Since
v2is part ofLibrary's inline namespace set, the new definition extends the pre-existingLibrary::v2—that's thethat namespacein this scenario.
When that namespace Doesn't Apply
If the identifier in your named-namespace-definition doesn't resolve to a pre-existing namespace meeting the clause's criteria (e.g., it's a new name, or it's a namespace alias), the clause's first scenario doesn't trigger. Instead, you're creating a new namespace, and that namespace has no applicable target here.
内容的提问来源于stack exchange,提问作者João Afonso

