std :: shared_ptr :: owner_before和std :: owner_less:“基于所有者的订单”的确切含义是什么? [英] std::shared_ptr::owner_before and std::owner_less: What exactly is meant by "owner-based order"?

查看:117
本文介绍了std :: shared_ptr :: owner_before和std :: owner_less:“基于所有者的订单”的确切含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对此进行了一些讨论,但是似乎没有任何东西可以说明基于所有者的订单实际上是什么。

I have found a few discussions on this but nothing appears to ever specify what "owner based order" actually is.

是否有效地评估< 所拥有的指针存储器地址的值?

Is it effectively evaluating < on the value of the owned pointer memory addresses?

推荐答案

它定义了一个任意的严格弱排序,根据该弱排序,当且仅当两个指针拥有所有权或都为空时,两个指针才等效。

It defines an arbitrary strict weak ordering under which two pointers are equivalent if and only if they share ownership or are both empty.

等效性用通常的方式定义:

Equivalence is defined in the usual way:

bool equivalent(p1, p2) {
    return !p1.owner_before(p2) && !p2.owner_before(p1);
}

这并不一定意味着它们指向同一对象。两个指针可以指向不同的对象,但仍然共享所有权:

This doesn't necessarily mean that they point to the same object. Two pointers can point to different objects but still share ownership:

struct thing {int n;};
shared_ptr<thing> t1 = make_shared<thing>();
shared_ptr<int>   t2(t1, &t1->n);

assert(t1 != t2);          // point to different objects
assert(equivalent(t1,t2)); // share ownership

同样,两个指针可以指向同一对象而无需共享:

Likewise, two pointers can point to the same object without sharing:

thing t;
shared_ptr<thing> t1(&t, some_deleter());
shared_ptr<thing> t2(&t, some_deleter());

assert(t1 == t2);            // point to the same object
assert(!equivalent(t1, t2)); // don't share ownership

(当然,默认删除器会造成灾难性的会尝试删除该对象;但是对于此类事情,有一个合适的自定义删除器是明智的应用程序。)

(Of course, this would be disastrous with the default deleter since both would try to delete the object; but there are sensible applications for this kind of thing with a suitable custom deleter).

实际上,可以通过比较地址来实现共享引用计数所使用的内部结构。

In practice, this could be implemented by comparing the address of the internal structure used for the shared reference count.

这篇关于std :: shared_ptr :: owner_before和std :: owner_less:“基于所有者的订单”的确切含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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