gpt4 book ai didi

c++ - 如何比较指针?

转载 作者:IT老高 更新时间:2023-10-28 12:10:33 26 4
gpt4 key购买 nike

假设我有 2 个指针:

int *a = something;
int *b = something;

如果我想比较它们,看看它们是否指向同一个地方,(a == b) 是否有效?

最佳答案

关于事实这里是规范中的相关文本

等式运算符 (==,!=)

可以将指向相同类型对象的指针与“直观”的预期结果进行比较:

来自 C++11 标准的 § 5.10:

Pointers of the same type (after pointer conversions) can be compared for equality. 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 (3.9.2).

(leaving out details on comparison of pointers to member and or the null pointer constants - they continue down the same line of 'Do What I Mean':)

  • [...] If both operands are null, they compare equal. Otherwise if only one is null, they compare unequal.[...]

The most 'conspicuous' caveat has to do with virtuals, and it does seem to be the logical thing to expect too:

  • [...] if either is a pointer to a virtual member function, the result is unspecified. Otherwise they compare equal if and only if they would refer to the same member of the same most derived object (1.8) or the same subobject if they were dereferenced with a hypothetical object of the associated class type. [...]

关系运算符 (<,>,<=,>=)

来自 C++11 标准的 § 5.9:

Pointers to objects or functions of the same type (after pointer conversions) can be compared, with a result defined as follows:

  1. If two pointers p and q of the same type point to the same object or function, or both point one past the end of the same array, or are both null, then p<=q and p>=q both yield true and p<q and p>q both yield false.
  2. 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.
  3. If two pointers point to non-static data members of the same object, or to subobjects or array elements of such members, recursively, the pointer to the later declared member compares greater provided the two members have the same access control (Clause 11) and provided their class is not a union.
  4. If two pointers point to non-static data members of the same object with different access control (Clause 11) the result is unspecified.
  5. If two pointers point to non-static data members of the same union object, they compare equal (after conversion to void*, if necessary). If two pointers point to elements of the same array or one beyond the end of the array, the pointer to the object with the higher subscript compares higher.
  6. Other pointer comparisons are unspecified.

所以,如果你有:

int arr[3];
int *a = arr;
int *b = a + 1;
assert(a != b); // OK! well defined

也可以:

struct X { int x,y; } s;
int *a = &s.x;
int *b = &s.y;
assert(b > a); // OK! well defined

但这取决于 something 在你的问题中:

int g; 
int main()
{
int h;
int i;

int *a = &g;
int *b = &h; // can't compare a <=> b
int *c = &i; // can't compare b <=> c, or a <=> c etc.
// but a==b, b!=c, a!=c etc. are supported just fine
}

奖励:标准库中还有什么?

§ 20.8.5/8:“对于模板 greaterlessgreater_equalless_equal,任何指针类型的特化都会产生一个总顺序,即使内置运算符 < , > , <= , >= 没有。”

所以,您可以全局订购任何奇数void*只要你用std::less<>和 friend ,不裸operator< .

关于c++ - 如何比较指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9086372/

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