gpt4 book ai didi

c++ - 不合格的名称查找 : Why local declaration hides declaration from using directive

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:29:53 26 4
gpt4 key购买 nike

考虑这段代码:

namespace A
{
int i = 24;
}

namespace B
{
using namespace A;
int i = 11;

int k = i; // finds B::i, no ambiguity
}

basic.lookup.unqual.2 :

§6.4.1 Unqualified name lookup [basic.lookup.unqual]

  1. The declarations from the namespace nominated by a using-directive become visible in a namespace enclosing the using-directive; see [namespace.udir]. For the purpose of the unqualified name lookup rules described in [basic.lookup.unqual], the declarations from the namespace nominated by the using-directive are considered members of that enclosing namespace.

对我来说,标准说得很清楚,为了不合格的名称查找(int k = i 中的 i)i 的声明A 中的 code> 被认为是 B 的成员,因此 iint k = i 中应该是不明确的,但是两者gccclang compile并将 i 解析为本地 B::i。我搜索了标准( basic.scope.hidingnamespace.udir ),没有发现与上述标准相矛盾的异常(exception)或规则。我发现对于合格的名称查找,但对于不合格的名称查找则不然。

为什么 i 是明确的?

最佳答案

关键是 10.3.4/2 “在非限定名称查找期间,名称看起来好像是在最近的封闭命名空间中声明的,该命名空间同时包含 using 指令和指定的命名空间。”

指定的命名空间在A,using指令在B,最小的(实际上只是)公共(public)命名空间是全局命名空间。因此 i 看起来就像在全局命名空间中声明的一样,并被 B::i 隐藏。

关于c++ - 不合格的名称查找 : Why local declaration hides declaration from using directive,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48359856/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com