【问题标题】:Creating boost::thread with an std::shared_ptr object instance使用 std::shared_ptr 对象实例创建 boost::thread
【发布时间】:2012-07-21 06:22:39
【问题描述】:

我有以下两个代码段。第一个块编译并按预期工作。但是第二个块没有编译。

我的问题是,鉴于下面的代码,当尝试基于由 shared_ptr 代理的对象实例创建线程时,正确的语法是什么?

#include <iostream>
#include <new> 
#include <memory>

#include <boost/thread.hpp>

struct foo
{
   void boo() {}
};

int main()
{
   //This works
   {
      foo* fptr = new foo;
      boost::thread t(&foo::boo,fptr);
      t.join();
      delete fptr;
   }

   //This doesn't work
   {
      std::shared_ptr<foo> fptr(new foo);
      boost::thread t(&foo::boo,fptr);
      t.join();
   }

   return 0;
}

编译器错误:

Error   5   error C2784: 'T *boost::get_pointer(T *)' : could not deduce template argument for 'T *' from 'std::tr1::shared_ptr<_Ty>'   c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp  40  1   htest
Error   3   error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could not deduce template argument for 'const std::auto_ptr<_Ty> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp  40  1   htest
Error   4   error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could not deduce template argument for 'const std::auto_ptr<_Ty> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp  40  1   htest
Error   8   error C2784: 'T *boost::get_pointer(const boost::shared_ptr<X> &)' : could not deduce template argument for 'const boost::shared_ptr<X> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp  40  1   htest
Error   9   error C2784: 'T *boost::get_pointer(const boost::shared_ptr<X> &)' : could not deduce template argument for 'const boost::shared_ptr<X> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp  40  1   htest
Error   1   error C2784: 'T *boost::get_pointer(const boost::scoped_ptr<T> &)' : could not deduce template argument for 'const boost::scoped_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp  40  1   htest
Error   2   error C2784: 'T *boost::get_pointer(const boost::scoped_ptr<T> &)' : could not deduce template argument for 'const boost::scoped_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>' c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp  40  1   htest
Error   6   error C2784: 'T *boost::get_pointer(const boost::reference_wrapper<T> &)' : could not deduce template argument for 'const boost::reference_wrapper<T> &' from 'std::tr1::shared_ptr<_Ty>'   c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp  40  1   htest
Error   7   error C2784: 'T *boost::get_pointer(const boost::reference_wrapper<T> &)' : could not deduce template argument for 'const boost::reference_wrapper<T> &' from 'std::tr1::shared_ptr<_Ty>'   c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp  40  1   htest
Error   10  error C2784: 'T *boost::get_pointer(const boost::intrusive_ptr<T> &)' : could not deduce template argument for 'const boost::intrusive_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>'   c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp  40  1   htest
Error   11  error C2784: 'T *boost::get_pointer(const boost::intrusive_ptr<T> &)' : could not deduce template argument for 'const boost::intrusive_ptr<T> &' from 'std::tr1::shared_ptr<_Ty>'   c:\program files (x86)\boost\boost_1_47\boost\bind\mem_fn_template.hpp  40  1   htest

【问题讨论】:

    标签: c++ boost c++11 shared-ptr boost-thread


    【解决方案1】:

    问题在于boost::thread 依赖boost::mem_fn 来处理成员函数,而boost::mem_fn(或至少您正在使用的版本)不知道如何使用std::shared_ptr 来调用成员函数因为它希望您在错误列表中使用boost::shared_ptr 或无数其他智能指针类型之一。

    原始指针有效,因为boost::mem_fn 已经有该重载。解决方案是使用boost::shared_ptrstd::mem_fn。后者有效,因为std::mem_fn 知道如何与std::shared_ptr 交互

    boost::thread t(std::mem_fn(&amp;foo::boo), fptr);

    【讨论】:

      【解决方案2】:

      Dave S 的答案的替代方法是定义这个(之前 &lt;boost/mem_fn.hpp&gt; 被包括在内):

      namespace boost
      {
        template<typename T>
          inline T*
          get_pointer(const std::shared_ptr<T>& p)
          { return p.get(); }
      }
      

      “教导”boost::mem_fnstd::shared_ptr 获取原始指针。

      在 C++11 中,std::mem_fn 需要与任何类似指针的类型一起工作,只需解除对它的引用,即*fptr,但boost::mem_fn 改为使用*boost::get_pointer(fptr)。我不知道它是否在最新版本的 Boost 中得到修复,但我认为它应该使用 SFINAE 来检测 get_pointer 是否可以工作,否则应该取消引用它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-07
        • 2017-03-22
        • 1970-01-01
        • 2016-03-15
        • 1970-01-01
        • 1970-01-01
        • 2020-12-21
        • 2011-09-27
        相关资源
        最近更新 更多