C++ 绑定参数复制行为

C++ 绑定参数复制行为,c++,c++11,stdbind,C++,C++11,Stdbind,当异步调用函数对象时,将std::shared_ptr传递给std::bind是否安全 即,大致如下所示: // Copy do not reference shared_ptr void someFunc(std::shared_ptr<Something> arg1,...other args...); std::shared_ptr<Something> data; // This may go out of scope before the functor be

当异步调用函数对象时,将
std::shared_ptr
传递给
std::bind
是否安全

即,大致如下所示:

// Copy do not reference shared_ptr
void someFunc(std::shared_ptr<Something> arg1,...other args...);

std::shared_ptr<Something> data; // This may go out of scope before the functor below is called
auto myFuture = QtConcurrent::run(std::bind(&someFunc,data,...other args...)); // In this case using QT but could be anything else
//复制不引用共享\u ptr
void someFunc(std::shared_ptr arg1,…其他arg…);
std::共享的ptr数据;//在调用下面的函子之前,这可能超出范围
auto myFuture=QtConcurrent::run(std::bind(&someFunc,data,…其他参数…);//在这种情况下,可以使用QT,但也可以是其他任何东西
我认为as
std::bind
通过引用获取其参数,上述内容不安全,但需要确认

作为记录,我使用了两个编译器:

  • 叮当7(操作系统X)
  • VC++12.0(即Visual Studio 2013)

  • std::bind
    返回的函数对象存储绑定参数的副本,因此就
    数据的生存期而言,您的代码是安全的。

    std::bind返回的函数对象存储绑定参数的副本,因此,就
    数据的生命周期而言,您的代码是安全的。

    为什么
    std::bind
    获取其参数很重要?当然,重要的是它是否将它们的副本放入它返回的东西中,而不是如何获取它们。这一点很好。这是代表我的拙劣措辞。我关心的是,返回的functor是否保留了一个副本或引用,T.C.在下面给出了很好的回答。为什么
    std::bind
    如何获取其参数很重要?当然,重要的是它是否将它们的副本放入它返回的东西中,而不是如何获取它们。这一点很好。这是代表我的拙劣措辞。我关心的是返回的函子是否保存了副本或引用,T.C.在下面给出了很好的回答。