为什么 enable_shared_from_this 无法直接访问嵌入式weak_ptr? [英] Why does enable_shared_from_this lack direct access to the embedded weak_ptr?

查看:62
本文介绍了为什么 enable_shared_from_this 无法直接访问嵌入式weak_ptr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在多线程应用程序中使用具有自动连接管理功能的 boost 信号2.我的类继承自 enable_shared_from_this<> 并且我想从另一个成员方法中连接一个成员方法.连接可能会经常重建,所以我的代码应该尽可能快(尽管有提升信号 2 的性能):

I want to use boost signals2 with automatic connection management in a multithreaded application. My class inherits from enable_shared_from_this<> and i want to connect a member method from within another member method. The connection might be rebuilt frequently so my code should be as fast as possible (despite from the boost signals2 performance itself):

typedef boost::signals2::signal<void ()> signal_type;

struct Cat : public enable_shared_from_this<Cat>
{
  void meow ();

  void connect (signal_type& s)
  {
    // can't write this
    s.connect (signal_type::slot_type (&Cat::meow, this, _1).track (weak_from_this ()));

    // ok, but slow?! two temporary smart pointers
    weak_ptr<Cat> const myself (shared_from_this ());
    s.connect (signal_type::slot_type (&Cat::meow, this, _1).track (myself));
  }

  // i am missing something like this in the base class
  // protected:
  //   weak_ptr<Cat> const& weak_from_this ();
};

我知道我的设计目标可能存在冲突(自动连接管理和线程安全以及快速代码)但无论如何:

I know that my design goals might be conflicting (automatic connection management and thread safety but also fast code) but anyway:

  1. 为什么 enable_shared_from_this<> 无法直接访问嵌入的 weak_ptr<>?我看不出相反的理由.有没有和我类似的用例?

  1. Why does enable_shared_from_this<> lack direct access to the embedded weak_ptr<>? I can't see an opposing reason. Is there no use case similar to mine?

有没有比上面更快的解决方法?

Is there a faster workaround than the one above?

我知道我可以这样做,但我想避免额外的存储/初始化检查惩罚:

I know i can do somethink like this, but i want to avoid the additional storage/init-check penalty:

template <typename T>
struct enable_weak_from_this : public enable_shared_from_this<T>
{
protected:
  weak_ptr<T> /* const& */ weak_from_this ()
  {
    if (mWeakFromThis.expired ())
    {
      mWeakFromThis = this->shared_from_this ();
    }

    return mWeakFromThis;
  }

private:
  weak_ptr<T> mWeakFromThis;
};

推荐答案

您无权访问 weak_ptr 的原因是 enable_shared_from_this 没有必须使用一个.拥有weak_ptr 只是enable_shared_from_this 的一种可能 实现.它不是唯一的.

The reason you don't have access to the weak_ptr is that enable_shared_from_this doesn't have to use one. Having a weak_ptr is simply one possible implementation of enable_shared_from_this. It is not the only one.

由于 enable_shared_from_thisshared_ptr 属于同一标准库的一部分,因此可以使用比直接存储 weak_ptr 更有效的实现.委员会不想阻止这种优化.

Since enable_shared_from_this is part of the same standard library as shared_ptr, a more efficient implementation could be used than directly storing a weak_ptr. And the committee doesn't want to prevent that optimization.

//好的,但是很慢?!两个临时智能指针

// ok, but slow?! two temporary smart pointers

那只是一个临时的智能指针.复制省略/移动应该处理除第一个对象之外的任何内容.

That's only one temporary smart pointer. Copy elision/movement should take care of anything but the first object.

这篇关于为什么 enable_shared_from_this 无法直接访问嵌入式weak_ptr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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