gpt4 book ai didi

C++ 名称查找 - 来自标准的示例

转载 作者:搜寻专家 更新时间:2023-10-31 02:21:09 24 4
gpt4 key购买 nike

我需要标准 [basic.lookup.unqual]/3 对这个例子的解释:

typedef int f;
namespace N {
struct A {
friend void f(A &);
operator int();
void g(A a) {
int i = f(a); // f is the typedef, not the friend
// function: equivalent to int(a)
}
};
}

我认为 void N::f(A &)int(a) 更接近,因为它不涉及类型转换运算符。但是我不能确定,因为该标准只包含一个“类型转换”实例。


顺便说一句,在 MSVC2015 中编译该代码失败(但它在 clang 和 g++ 中有效)。

Error C2440 'initializing': cannot convert from 'void' to 'int'


更新解决一些评论。

  1. 类型转换的正式名称为“类型转换”,它们在 (12.3) 中有所介绍(感谢 jeffrey)。

  2. 我正在寻找的是语法分析的描述。特别是,为什么 postfix-expression ( expression-list opt )simple-type-specifier ( expression-list opt ) 践踏。因为根据 (5.2),这两个表达式都是从左到右求值的。因此,在 (a) 之前的两个候选者中,::N::f 应该比 ::f 更接近时评估 ::N::A::g 中的表达式。

最佳答案

“类型转换”与这种情况无关。参数相关查找的规则包括,来自 [basic.lookup.argdep]:

Let X be the lookup set produced by unqualified lookup (3.4.1) and let Y be the lookup set produced by argument dependent lookup (defined as follows). If X contains
(3.1) — a declaration of a class member, or
(3.2) — a block-scope function declaration that is not a using-declaration, or
(3.3) — a declaration that is neither a function or a function template
then Y is empty. Otherwise Y is the set of declarations found in the namespaces associated with the argument types as described below. The set of declarations found by the lookup of the name is the union of X and Y.

f 的非限定查找产生的查找集是:

typedef int f;

该声明既不是函数也不是函数模板,因此 Y 为空。我们不考虑友元函数 f,因为它对非限定查找不可见。

关于C++ 名称查找 - 来自标准的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31772588/

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