c++ - 使用 std::async 调用采用 unique_ptr 的函数

标签 c++ c++11

我正在尝试让 std::async 启动一个函数,该函数接受带有 gcc 4.6.2 的 unique_ptr:

#include <type_traits>
#include <memory>
#include <functional>
#include <future>

struct Foo {};

// my gcc does not have this, same definition as in 30.2.6
template<typename T>
typename std::decay<T>::type decay_copy(T&& v) { return std::forward<T>(v); }

int main() {
  std::function<int(std::unique_ptr<Foo>)> f = [](std::unique_ptr<Foo>) { return 23; };

  std::unique_ptr<Foo> fp{new Foo};
  std::unique_ptr<Foo> fp2{std::move(fp)};
  // works, this seems to satisfy the requirements of async
  f(decay_copy(std::move(fp2)));

  // does not work with reference to a deleted function
  // std::async(f, std::move(fp2));
}

这是预期的行为还是 gcc 错误?

最佳答案

可能是一个错误。它可以用 g++ 4.7 编译。

关于c++ - 使用 std::async 调用采用 unique_ptr 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8424112/

相关文章:

c++ - 关于C++0x中右值的问题

c++ - C++11 中的 exec 缺少 "missing sentinel"警告

c++ - 为什么 std::move 需要前向引用?

c++ - 错误 : reference to overloaded function could not be resolved; did you mean to call it?

c++ - 如何从 wchar_t* 转换为 wstring?

c++ - 获取和设置相机设置

C++ 使用多重集进行二优先级排序

c++ - 通过引用传递 vector - 段错误 C++

c++ - ifstream 随机整数?

C++更改构造函数中的函数解析优先级