gpt4 book ai didi

C++ 11初始化程序具有模棱两可的函数id表达式?

转载 作者:IT老高 更新时间:2023-10-28 22:04:14 24 4
gpt4 key购买 nike

在以下 C++11 代码中:

void f(int) {}
void f(double) {}

void (*p)(int) = f;

有两个功能。

第三个f标识符是一个id-expression和p的初始化器。

在 5.1.1p8 [expr.prim.general]/8 中它说:

The type of the [id-expression] is the type of the identifier. The result is the entity denoted by the identifier. The result is an lvalue if the entity is a function, variable, or data member and a prvalue otherwise.

鉴于 f 可能指代具有两种不同类型的两个不同实体,因此没有“实体”或“类型”。

标准中是否还有其他文本可以解决这种情况?

实现只是将其作为扩展消除歧义,还是在某个地方需要它? (如果没有其他文本,人们可能会争辩说,一个实现可能会拒绝 f id 表达式为模棱两可。)

最佳答案

标准(第 13.4 节)定义:

A use of an overloaded function name without arguments is resolved in certain contexts to a function, a pointer to function or a pointer to member function for a specific function from the overload set. A function template name is considered to name a set of overloaded functions in such contexts. The function selected is the one whose type is identical to the function type of the target type required in the context.

强调我的。

在引用之后,有一个与您的类似的示例(第 13.4/5 节):

int f(double);
int f(int);
int (*pfd)(double) = &f; // selects f(double)
int (*pfi)(int) = &f; // selects f(int)

就一元 & 而言,标准规定(在 § 5.3.1/6 并感谢 jogojapan ):

The address of an overloaded function can be taken only in a context that uniquely determines which version of the overloaded function is referred to.

但也可以省略(第 13.4/1 节):

The overloaded function name can be preceded by the & operator.

(再次强调我的)就像你在你的例子中所做的那样。

关于C++ 11初始化程序具有模棱两可的函数id表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20652424/

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