加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如果一个过期的weak_ptr会给出未定义的行为,那么owner_less

发布时间:2020-12-16 05:22:37 所属栏目:百科 来源:网络整理
导读:请考虑我的经验,但我不明白std :: owner_less的要点. 我已经是shown,因为没有推荐使用weak_ptr作为关键字的地图,因为过期的weak_ptr密钥会破坏地图,实际上是: If it expires,then the container’s order is broken,and trying to use the container afterw
请考虑我的经验,但我不明白std :: owner_less的要点.

我已经是shown,因为没有推荐使用weak_ptr作为关键字的地图,因为过期的weak_ptr密钥会破坏地图,实际上是:

If it expires,then the container’s order is broken,and trying to use the container afterwards will give undefined behaviour.

这个行为有多么不明确?我问的原因是因为docs说关于owner_less:

This function object provides owner-based (as opposed to value-based) mixed-type ordering of both std::weak_ptr and std::shared_ptr. The order is such that two smart pointers compare equivalent only if they are both empty or if they both manage the same object,even if the values of the raw pointers obtained by get() are different (e.g. because they point at different subobjects within the same object)

再次,这是我的缺乏经验的谈话,但听起来不像地图将被完全打破expired weak_ptr

Returns whether the weak_ptr object is either empty or there are no more shared_ptr in the owner group it belongs to.

Expired pointers act as empty weak_ptr objects when locked,and thus can no longer be used to restore an owning shared_ptr.

听起来,它可能变得比完全没有定义更为松软.如果一个人的实现删除了过期的weak_ptrs,并且根本没有或没有用于任何徘徊的行为,那么行为何时会被定义?

如果一个人的执行不考虑订单,但只需要一个方便的方法来将weak_ptrs与数据相关联,行为仍然是未定义的?换句话说,会发现开始返回错误的键?

地图

在文档中我可以找到的唯一问题是上面引用的,那个过期的weak_ptrs将返回等效项.

根据这些docs,对于不依赖订购的实现而言,这对于过期的weak_ptrs来说并不是一个问题:

Associative

Elements in associative containers are referenced by their key and not by their absolute position in the container.

Ordered

The elements in the container follow a strict order at all times. All inserted elements are given a position in this order.

Map

Each element associates a key to a mapped value: Keys are meant to identify the elements whose main content is the mapped value.

这听起来像一个实现不关心顺序,也没有使用过期的weak_ptrs,所以没有问题,因为值不是通过键引用的值,所以找到一个过期的weak_ptr可能会返回另一个weak_ptrs值,但是由于没有用它在这个特殊的实现中除了要被擦除,没有问题.

我可以看到如何使用weak_ptr排序或过期的weak_ptrs可能是一个问题,无论应用程序可能是什么,但所有行为似乎远未定义,所以地图或集合似乎并没有完全被一个过期的weak_ptr打破.

是否有更多的技术解释的地图,weak_ptr和owner_less反驳这些文件和我的解释?

解决方法

一点澄清.使用owner_less时,已过期的weak_ptr不是UB.从标准

under the equivalence relation defined by operator(),!operator()(a,
b) && !operator()(b,a),two shared_ptr or weak_ptr instances are
equivalent if and only if they share ownership or are both empty.

有一件事要记住的是,一个空的weak_ptr是一个从未被分配过一个有效的shared_ptr,或者被分配了一个空的shared_ptr / weak_ptr的一个.已过期的weak_ptr不是空的weak_ptr.

编辑:

上面的定义取决于有一个“空”weak_ptr是什么意思.所以,我们来看标准

  • constexpr weak_ptr() noexcept;

    Effects: Constructs an empty weak_ptr object.
    Postconditions: use_count() == 0.

  • weak_ptr(const weak_ptr& r) noexcept;
  • template weak_ptr(const weak_ptr& r) noexcept;
  • template weak_ptr(const shared_ptr& r) noexcept;

    Requires: The second and third constructors shall not participate in the overload resolution unless Y* is implicitly convertible to T*.

    Effects: If r is empty,constructs an empty
    weak_ptr object; otherwise,constructs a weak_ptr object that shares
    ownership with r and stores a copy of the pointer stored in r.

    Postconditions: use_count() == r.use_count().

交换简单地交换内容,赋值被定义为上述构造函数和交换.

要创建一个空的weak_ptr,你使用默认的构造函数,或者传递一个为空的weak_ptr或shared_ptr.现在,您将注意到,过期并不会导致weak_ptr变为空.它只是使它有一个use_count()为零和expired()返回true.这是因为在释放共享对象的所有弱指针之前,无法释放底层引用计数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读