是否可以确保弱指针在std :: shared_ptr删除程序运行时已过期? [英] Are weak pointers guaranteed to have expired by the time the std::shared_ptr deleter runs?

查看:79
本文介绍了是否可以确保弱指针在std :: shared_ptr删除程序运行时已过期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个带有自定义删除器的 std :: shared_ptr< Foo> ,是否可以保证所有相关的弱指针都被删除器视为已过期? (如果您可以引用标准中的相关部分,将不胜感激。)

If I have a std::shared_ptr<Foo> with a custom deleter, is it guaranteed that all associated weak pointers are seen as expired by the deleter? (I would appreciate it very much if you could cite relevant sections in the standard.)

换句话说,下面的断言是否可以保证不会触发?

In other words is the assertion below guaranteed not to fire?

std::weak_ptr<Foo> weak;
std::shared_ptr<Foo> strong{
  new Foo,
  [&weak] (Foo* f) {
    assert(weak.expired());
    delete f;
  },
};

weak = strong;
strong.reset();


推荐答案

C ++ 14标准中显然没有任何内容这保证了这一点。我现在打开了缺陷报告,以解决该问题。

There is apparently nothing in the C++14 standard that guarantees this. I've now opened a defect report for the standard covering the problem.

这篇关于是否可以确保弱指针在std :: shared_ptr删除程序运行时已过期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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