gpt4 book ai didi

c++ - 比较指向不同数组的指针是否相等是未指定的行为吗?

转载 作者:IT老高 更新时间:2023-10-28 22:17:40 25 4
gpt4 key购买 nike

等式运算符具有关系运算符对指针的语义限制:

The == (equal to) and the != (not equal to) operators have the same semantic restrictions, conversions, and result type as the relational operators except for their lower precedence and truth-value result. [C++03 §5.10p2]

并且关系运算符对比较指针有限制:

If two pointers p and q of the same type point to different objects that are not members of the same object or elements of the same array or to different functions, or if only one of them is null, the results of p<q, p>q, p<=q, and p>=q are unspecified. [§5.9p2]

这是由相等运算符“继承”的语义限制吗?

具体来说,给定:

int a[42];
int b[42];

很明显 (a + 3) < (b + 3) 是未指定的,但 (a + 3) == (b + 3) 是否也未指定?

最佳答案

op==op!= 的语义明确表示映射是除了它们的真值结果。因此,您需要查看为它们的真值结果定义的内容。如果他们说结果是未指定的,那么它是未指定的。如果他们定义了特定的规则,那么它不是。它特别说

Two pointers of the same type compare equal if and only if they are both null, both point to the same function, or both represent the same address

关于c++ - 比较指向不同数组的指针是否相等是未指定的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4909766/

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