绑定到weak_ptr [英] Binding to a weak_ptr

查看:202
本文介绍了绑定到weak_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有办法 std :: bind to std :: weak_ptr 吗?我想存储一个弱函数回调,当被调用者被销毁时自动断开。

Is there a way to std::bind to a std::weak_ptr? I'd like to store a "weak function" callback that automatically "disconnects" when the callee is destroyed.

我知道如何创建一个std ::函数使用shared_ptr:

I know how to create a std::function using a shared_ptr:

std::function<void()> MyClass::GetCallback()
{
    return std::function<void()>(std::bind(&MyClass::CallbackFunc, shared_from_this()));
}

但是返回的std ::函数使我的对象永远活着。所以我想绑定到 weak_ptr

However the returned std::function keeps my object alive forever. So I'd like to bind it to a weak_ptr:

std::function<void()> MyClass::GetCallback()
{
    std::weak_ptr<MyClass> thisWeakPtr(shared_from_this());
    return std::function<void()>(std::bind(&MyClass::CallbackFunc, thisWeakPtr));
}

但这不编译。 (std :: bind将不接受weak_ptr!)有没有任何方式绑定到weak_ptr?

But that doesn't compile. (std::bind will accept no weak_ptr!) Is there any way to bind to a weak_ptr?

我已经找到关于这个问题的讨论似乎没有标准的实现。

I've found discussions about this (see below), but there seems to be no standard implementation. What is the best solution for storing a "weak function", in particular if Boost is not available?

讨论/研究是什么?存储弱函数的最佳解决方案是什么? (所有这些都使用Boost并且不标准化):

Discussions / research (all of these use Boost and are not standardized):

  • weak_function
  • weak_ptr binding
  • "weak" binding (and a fix for it)
  • weak_fn
  • Another weak_fn

推荐答案

std::weak_ptr<MyClass> thisWeakPtr(shared_from_this());
return std::function<void()>(std::bind(&MyClass::CallbackFunc, thisWeakPtr));

你不应该这样做。

MyClass :: CallbackFunc 是非静态成员函数class MyClass 。作为非静态成员函数,必须使用 MyClass 有效实例

MyClass::CallbackFunc is a non-static member function of the class MyClass. Being a non-static member function, it must be called with a valid instance of MyClass.

weak_ptr 整个点不是必需的有效。您可以通过将其转换为 shared_ptr 来检测其有效性,然后测试指针是否为NULL。由于 weak_ptr 无法保证始终有效,因此不能使用

The entire point of weak_ptr is that it isn't necessarily valid. You can detect its validity by transforming it into a shared_ptr and then testing if the pointer is NULL. Since weak_ptr is not guaranteed to be valid at all times, you cannot call a non-static member function with one.

你正在做什么不比以下更有效:

What you're doing is no more valid than:

std::bind(&MyClass::CallbackFunc, nullptr)

它可以编译,但最终会崩溃,调用它。

It may compiled, but it will eventually crash when you try to call it.

最好的办法是使用实​​际的逻辑,如果 weak_ptr 无效。 bind 不是为了做逻辑;它只是完全正确地你告诉它:调用函数。所以你需要使用正确的lambda:

Your best bet is to use actual logic, to not call the callback function if the weak_ptr is not valid. bind is not designed to do logic; it just does exactly what you tell it to: call the function. So you need to use a proper lambda:

std::weak_ptr<MyClass> thisWeakPtr(shared_from_this());
return std::function<void()>([thisWeakPtr]()
{
  auto myPtr = thisWeakPtr.lock();
  if(myPtr)
    myPtr->CallbackFunc()
});

这篇关于绑定到weak_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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