您可以将nullptr与其他指针进行比较吗?它总是更小吗? [英] Can you compare nullptr to other pointers for order? Is it always smaller?

查看:94
本文介绍了您可以将nullptr与其他指针进行比较吗?它总是更小吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题基于我发现的用于监视可能的内存泄漏的代码,因此它包含一些您可能不希望在常规程序中看到的代码,例如排序指针.

This question is based on code that I found that monitors possible memory leaks, so it contains some code that you probably don't want to see in regular programs like ordering pointers.

但是,我看到一个指针被设置为 nullptr ,然后将该指针与一个最大地址进行比较.C ++标准是否保证 nullptr 总是小于 operator< 的其他指针?

However, I saw that a pointer was set to nullptr and then the pointer was compared to a maximum address. Is it guaranteed by the C++ standard that nullptr is always smaller than other pointers for operator<?

推荐答案

nullptr总是比运算符的其他指针<

nullptr is always smaller than other pointers for operator<

否,标准不支持,将 nullptr 与关系运算符的指针进行比较.

No, compare a nullptr with a pointer by a relational operator is not supported by the standard.

要比较关系运算符的操作数,首先将以下规则应用于两个操作数,即 expr.rel#2

To compare the operands of a relational operator, the following rule will first be applied to both operands, that is expr.rel#2

通常的算术转换是对算术或枚举类型的操作数执行的.如果两个操作数都是指针,则将执行指针转换和限定转换以将它们带入其复合指针类型.转换后,操作数应具有相同的类型.

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

nullptr 不是指针,而是称为空指针常量.因此,执行指针转换和资格转换以将它们带入其复合指针类型".不适用于它.因此,它违反了规则.转换后,操作数应具有相同的类型.

nullptr is not a pointer, instead, it is called a null pointer constant. So, "pointer conversions and qualification conversions are performed to bring them to their composite pointer type" will not apply to it. So, it violates, After conversions, the operands shall have the same type.

C语给出了正确的诊断.由于代码格式不正确,因此谈论结果是没有意义的.

Clang gives a correct diagnosis. Since the code is ill-formed, hence talk about what's the result does not make sense.

这篇关于您可以将nullptr与其他指针进行比较吗?它总是更小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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