我想在多线程应用程序中使用带有自动连接管理的 boost signals2。我的类继承自enable_shared_from_this<>,我想从另一个成员方法中连接一个成员方法。连接可能会经常重建,因此我的代码应该尽可能快(尽管提升了信号2性能本身):
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 ();
};
我知道我的设计目标可能是冲突的(自动连接管理和线程安全以及快速代码),但无论如何:
- 为什么 - enable_shared_from_this<>缺乏对嵌入式的直接访问- weak_ptr<>?我看不到相反的理由。没有和我类似的用例吗?
- 有比上述更快的解决方法吗? 
编辑:
我知道我可以这样做,但我想避免额外的存储/初始化检查惩罚:
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;
};