You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

当前C++草案[namespace.def]/2中`that namespace`指代哪个命名空间?

Clarification of 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 A definition extends the original A—that namespace is exactly that original, pre-existing A namespace.

  • 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 v2 is part of Library's inline namespace set, the new definition extends the pre-existing Library::v2—that's the that namespace in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:13:14