nullptr"格式正确吗?-6ren"> nullptr"格式正确吗?-给定一个指针p: char *p ; // Could be any type 假设 p 已正确初始化,则格式如下: if (p > 0) // or p > nullptr 更一般地说,当一个操作数-6ren">
gpt4 book ai didi

c++ - 鉴于 p 是一个指针是 "p > nullptr"格式正确吗?

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

给定一个指针p:

char *p ; // Could be any type

假设 p 已正确初始化,则格式如下:

if (p > 0) // or p > nullptr

更一般地说,当一个操作数是指针而另一个操作数是空指针常量时,使用关系运算符是否格式正确?

最佳答案

在 C++14 中,此代码格式错误,但在 C++14 之前,此代码格式正确(但结果未指定),如 defect report 583: Relational pointer comparisons against the null pointer constant备注:

In C, this is ill-formed (cf C99 6.5.8):

void f(char* s) {
if (s < 0) { }
}

...but in C++, it's not. Why? Who would ever need to write (s > 0) when they could just as well write (s != 0)?

This has been in the language since the ARM (and possibly earlier); apparently it's because the pointer conversions (4.10 [conv.ptr]) need to be performed on both operands whenever one of the operands is of pointer type. So it looks like the "null-ptr-to-real-pointer-type" conversion is hitching a ride with the other pointer conversions.

在 C++14 中,N3624applied to the draft C++14 standard ,它是 N3478 的修订版。对 583 的建议解决方案说明:

This issue is resolved by the resolution of issue 1512.

问题 1512 建议的解决方法是 N3478(N3624 是 N3478 的修订版):

The proposed wording is found in document N3478.

第 5.9 节从 C++11 到 C++14 的更改

5.9 部分 关系运算符C++11 draft standard 之间发生了很大变化和 C++14 draft standard ,以下突出显示最相关的差异(强调我的 future ),来自第 1 段:

The operands shall have arithmetic, enumeration, or pointer type, or type std::nullptr_t.

更改为:

The operands shall have arithmetic, enumeration, or pointer type

所以类型 std::nullptr_t不再是有效的操作数,但仍然留下 0 这是一个 空指针常量,因此可以转换(section 4.10) 到一个指针类型

这被第 2 段所涵盖,在 C++11 中说:

[...]Pointer conversions (4.10) and qualification conversions (4.4) are performed on pointer operands (or on a pointer operand and a null pointer constant, or on two null pointer constants, at least one of which is non-integral) to bring them to their composite pointer type. If one operand is a null pointer constant, the composite pointer type is std::nullptr_t if the other operand is also a null pointer constant or, if the other operand is a pointer, the type of the other operand.[...]

this 为 空指针常量 操作数显式提供异常,在 C++14 中更改为以下内容:

The usual arithmetic conversions are performed on operands of arithmetic or enumeration type. If both operands are pointers, pointer conversions (4.10) and qualification conversions (4.4) are performed to bring them to their composite pointer type (Clause 5). After conversions, the operands shall have the same type.

在这种情况下,不允许将 0 转换为 指针类型。两个操作数都必须是指针才能应用指针转换,并且要求操作数在转换后具有相同的类型。这在一个操作数是指针类型而另一个是空指针常量 0的情况下是不满足的。

如果两个操作数都是指针但一个是空指针值怎么办?

R Sahu 提问,下面的代码格式正确吗?:

char* p = "";
char* q = nullptr;
if ( p > q ) {}

是的,在 C++14 中,这段代码格式正确,pq 都是指针,但比较的结果是未指定的。两个指针的定义比较在第 3 段中列出,并说:

Comparing pointers to objects is defined as follows:

  • If two pointers point to different elements of the same array, or to subobjects thereof, the pointer to the element with the higher subscript compares greater.

  • If one pointer points to an element of an array, or to a subobject thereof, and another pointer points one past the last element of the array, the latter pointer compares greater.

  • If two pointers point to different non-static data members of the same object, or to subobjects 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 段中它说:

[...]Otherwise, the result of each of the operators is unspecified.

在 C++11 中,它专门使 3 段中未指定结果:

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.

关于c++ - 鉴于 p 是一个指针是 "p > nullptr"格式正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26590267/

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