問題描述
相等運算符在指針上具有關系運算符的語義限制:
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和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
相同類型的兩個指針比較相等當且僅當它們都為空,都指向同一個函數,或者都代表同一個地址
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
這篇關于比較指向不同數組的指針是否相等是未指定的行為嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!