它是未指定的行为比较指针到不同的数组的平等? [英] Is it unspecified behavior to compare pointers to different arrays for equality?

查看:176
本文介绍了它是未指定的行为比较指针到不同的数组的平等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The equality operators have the semantic restrictions of relational operators on pointers:


==(等于)和!=不等于)运算符具有与关系运算符相同的语义限制,转换和结果类型,除了它们的较低优先级和真值结果。 [C ++ 03§5.10p2]

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]

关系运算符对比较指针有一个限制:

And the relational operators have a restriction on comparing pointers:


如果同一类型的两个指针p和q指向不是同一对象或同一数组或不同函数的元素的成员的不同对象,或者只有它们中的一个为空,p q,p <= q和p> = q的结果是未指定的。 [§5.9p2]

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]

这是一个语义限制,由相等运算符继承?

Is this a semantic restriction which is "inherited" by equality operators?

具体来说,如下:

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

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

It is clear that (a + 3) < (b + 3) is unspecified, but is (a + 3) == (b + 3) also unspecified?

推荐答案

op == op!= 的语义明确说明映射是值结果。所以你需要看看为它们的真值结果定义的是什么。如果他们说结果未指定,那么它是未指定的。如果他们定义特定的规则,那么它不是。它特别说明了

The semantics for op== and op!= explicitly say that the mapping is except for their truth-value result. So you need to look what is defined for their truth value result. If they say that the result is unspecified, then it is unspecified. If they define specific rules, then it is not. It says in particular


两个相同类型的指针当且仅当它们都为null时才比较相等,都指向同一个函数,或两者都代表相同的地址

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

这篇关于它是未指定的行为比较指针到不同的数组的平等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆