gpt4 book ai didi

c - 当 "pointer to an object type"与 "pointer to a void"比较相等时,如何理解转换规则?

转载 作者:行者123 更新时间:2023-11-30 21:16:04 26 4
gpt4 key购买 nike

n1570 6.5.9.5 (Equality operators)说:

5 ......If one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void, the former is converted to the type of the latter.

如果“前”是“后”是“指向对象类型的指针”和“指向 void 的指针”,则意味着在转换后,比较发生在两个 void* 上,根据 6.5.9.2,它是未定义的:

2.One of the following shall hold:

both operands have arithmetic type;

both operands are pointers to qualified or unqualified versions of compatible types;

one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void; or

one operand is a pointer and the other is a null pointer constant.

这是 N1570 中的语言缺陷吗?

最佳答案

If "former" is "latter" are "pointer to object type" and "pointer to a void", then it means, after the conversion, the compare happens on two void*s,

是的,我就是这么读的。

which according to 6.5.9.2, is undefined:

没有。您引用了标准第 6.5.9.2 节,显然是因为您相信您所询问的情况并未包含在该节的替代方案枚举列表中,但您错了。此替代方案涵盖以下情况:

one operand is a pointer to an object type and the other is a pointer to a qualified or unqualified version of void;

这里重要的是要理解指向 void 的指针指向对象类型的指针,例如

The void type comprises an empty set of values; it is an incomplete object type that cannot be completed.

(6.2.5/19;已添加强调)

6.5.9.2 不应被解释为暗示 void 不是对象类型;相反,它表示 void *所有对象指针类型相当,当然包括它本身。

正如 @SouravGhosh 首先观察到的,这种替代方案也涵盖了相关案例:

both operands are pointers to qualified or unqualified versions of compatible types

每种类型都与其自身兼容 (6.2.7/1)。

关于c - 当 "pointer to an object type"与 "pointer to a void"比较相等时,如何理解转换规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40381560/

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