在nullptr中使用std :: less [英] Using std::less with nullptr

查看:76
本文介绍了在nullptr中使用std :: less的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段中的断言是否总是成立?

Does the assertion in the following code snippet always hold?

std::less<Object *> lessPtr;
Object * o = new Object();
assert(lessPtr (o, nullptr) == false);


推荐答案

简介



这个问题实际上归结为在一个操作数是 nullptr 的指针类型上是否使用小于关系运算符将产生预期 结果;

Introduction

This question really boils down to whether the use of the less-than relational operator on pointer types where one operand is a nullptr will yield the "expected" result; which sadly isn't the case.

结果是未指定


注意:请注意, std :: less 保证了总订单;这意味着即使使用函数对象时结果为 unspecified ,每次调用也必须产生相同的 unspecified 值。

Note: Do mind that std::less guarantees a total order; meaning that even if the result, when using the function object, is unspecified, it must yield the same unspecified value on each invocation.






国际标准是什么( N3337 )说?




What does the International Standard (N3337) say?


5.9p2 关系运算符 [expr.rel]


可以比较指向相同类型的对象或函数的指针(在指针转换之后),其结果定义如下:

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


  • 如果两个相同类型的指针 p q 指向相同的对象或函数,或者都指向过去同一数组的末尾或均为null,则 p< = q p> = q true p< q p> q 都产生 false

  • 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.

如果两个指针 p 和<$相同类型的c $ c> q 指向不同对象,这些对象不是同一对象的成员,也不是同一数组的元素,或者指向不同的函数,或者如果其中只有一个为null,则结果 p< q p> q 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.

如果两个指针指向非静态相同对象的数据成员,或此类对象的子对象或数组元素,则递归地,如果两个成员具有相同的访问控制(第11条)并且它们的类不是联合,则指向后来声明的成员的指针比较大。

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.

如果两个指针指向具有不同访问控制的同一对象的非静态数据成员(第11条),则结果不确定。

If two pointers point to non-static data members of the same object with different access control (Clause 11) the result is unspecified.

如果两个指针指向同一联合对象的非静态数据成员,则它们比较相等(转换为 void * (如有必要)。如果两个指针指向同一数组的元素,或者指向数组末尾的一个指针,则指向下标较高的对象的指针的比较就更高。

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.

Other指针比较未指定。

Other pointer comparisons are unspecified.



20.8.5p8 比较 [比较]


对于模板更大 less greater_equal less_equal ,即使内置运算符< > ,< = > = 不要。

For templates greater, less, greater_equal, and less_equal, the specializations for any pointer type yield a total order, even if the built-in operators <, >, <=, >= do not.







那么,什么是真正的标准?




So, what is the standard really saying?

T * p = new T;
T * q = nullptr;


p<的结论是什么? q

p q 不会指向同一数组的不同元素(包括指向数组最后一个元素的元素),并且都不会指向同一对象的非静态数据成员; p<时的结果q (和 p> q )是未指定

What is the verdict for p < q?
Since p and q don't point to different elements of the same array (including the element one past the last element of an array), and both don't point to non-static data members of the same object; the result when doing p < q (and p > q) is unspecified.

bool a = p < q;  // unspecified
bool b = p < q;  // unspecified

assert (a == b); // can fire


std :: less 怎么样?

但是,使用 std :: less ,我们可以保证获得总订单-这实际上意味着以下断言不能触发( standard-20.8.5p8

What about std::less?
However, when using std::less we are guaranteed a total order - which effectively means that the below assertion cannot fire (standard-20.8.5p8).

std::less<T*> comp;

bool a = comp (p, q);  // unspecified
bool b = comp (p, q);  // unspecified

assert (a == b);       // can not fire

这篇关于在nullptr中使用std :: less的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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