假定p是指针,则"p"为"p". nullptr"格式正确? [英] Given that p is a pointer is "p > nullptr" well-formed?

查看:369
本文介绍了假定p是指针,则"p"为"p". nullptr"格式正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个指针p:

char *p ; // Could be any type

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

assuming p is properly initialized is the following well-formed:

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

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

More generally is it well-formed to use a relational operator when one operand is a pointer and the other is a null pointer constant?

推荐答案

在C ++ 14中,此代码格式错误,但在C ++ 14之前,这是格式正确的代码(,但结果是未指定),例如缺陷报告583:针对空指针常量的关系指针比较注意:

In C++14 this code is ill-formed but prior to the C++14 this was well-formed code(but the result is unspecified), as defect report 583: Relational pointer comparisons against the null pointer constant notes:

在C语言中,这是错误的格式(请参阅C99 6.5.8):

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

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

...但是在C ++中不是.为什么?谁需要写(s> 0) 他们什么时候可以写(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)?

这是自ARM(可能更早)以来的语言. 显然是因为指针转换(4.10 [conv.ptr])需要 只要其中一个操作数为 指针类型.因此它看起来像"null-ptr-to-real-pointer-type" 转换与其他指针转换搭便车.

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中,当适用于C ++ 14标准草案,它是对N3478的修订. 583注释的拟议决议:

In C++14 this was made ill-formed when N3624 was applied to the draft C++14 standard, which is a revision of N3478. The proposed resolution to 583 notes:

此问题已通过解决问题1512解决.

This issue is resolved by the resolution of issue 1512.

和问题1512提议的解决方案是N3478( N3624是N3478的修订版):

and issue 1512 proposed resolution is N3478(N3624 is a revision of N3478):

建议的措辞可以在文档N3478中找到.

The proposed wording is found in document N3478.

从C ++ 11更改为5.9节到C ++ 14

部分5.9 关系运算符 C ++ 14标准草案,以下着重强调了与1段最相关的区别(强调我的前进):

Section 5.9 Relational operators changed a lot between the C++11 draft standard and the C++14 draft standard, the following highlights the most relevant differences (emphasis mine going forward), from paragraph 1:

操作数应具有算术,枚举或指针类型,或 键入std :: nullptr_t .

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,它是 null指针常量,因此可以将其转换为 pointer类型( section 4.10 ).

So the type std::nullptr_t is no longer a valid operand but that still leaves 0 which is a null pointer constant and therefore can be converted(section 4.10) to a pointer type.

这在第2段中涉及,在C ++ 11中这样说:

This is covered by paragraph 2 which in C++11 says:

[...] 指针转换(4.10)和资格转换(4.4) 在指针操作数上执行(或在指针操作数和null上执行 指针常量,或两个空指针常量,至少其中之一 这是非整数),以将其带入其复合指针类型. 如果一个操作数是空指针常量,则复合指针类型 如果另一个操作数也是空指针常量,则为std :: nullptr_t 或者,如果另一个操作数是一个指针,则另一个的类型 操作数.[...]

[...]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.[...]

这明确为 null指针常量操作数提供了一个例外,在C ++ 14中更改为以下内容:

this explicitly provides an exception for a null pointer constant operand, changes to the following in C++14:

通常的算术转换是对 算术或枚举类型. 如果两个操作数都是指针,则指针 进行了转换(4.10)和资格转换(4.4) 将它们带入其复合指针类型(第5条). 之后 转换时,操作数应具有相同的类型.

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转换为指针类型.两个操作数都必须是指针,才能应用指针转换,并且转换后要求操作数具有相同的类型.在一个操作数是 pointer类型而另一个是 null指针常数 0的情况下,这是不能满足的.

In which there is no case that allows 0 to be converted to a pointer type. Both operands must be pointers in order for pointer conversions to be applied and it is required that the operands have the same type after conversions. Which is not satisfied in the case where one operand is a pointer type and the other is a null pointer constant 0.

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

R Sahu问,以下代码格式正确吗?:

R Sahu asks, is the following code well-formed?:

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

是的,在C ++ 14中,此代码格式正确,pq都是指针,但比较结果不确定.在3段中列出了两个指针的已定义比较,并说:

Yes, in C++14 this code is well formed, both p and q are pointers but the result of the comparison is unspecified. The defined comparisons for two pointers is set out in paragraph 3 and says:

将对象的指针进行比较的定义如下:

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.

如果两个指针递归指向同一对象的不同非静态数据成员或此类成员的子对象,则 指向后来声明的成员的指针比较大,前提是两者 成员具有相同的访问控制(第11条),并提供了 班级不是工会.

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段中将其定义为:

Null pointers values are not defined here and later on in paragraph 4 it says:

[...]否则,未指定每个运算符的结果.

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

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

In C++11 it specifically makes the results unspecified in paragraph 3:

如果两个相同类型的指针p和q指向不同的对象 不是同一对象的成员或同一数组的元素的成员 或对于不同的功能,;或者如果其中只有一个为空,则结果 未指定p< q,p>,p< = q和p> = q中的.

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.

这篇关于假定p是指针,则"p"为"p". nullptr"格式正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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