cameron314 / concurrentqueue

A fast multi-producer, multi-consumer lock-free concurrent queue for C++11
Other
9.86k stars 1.69k forks source link

An `std::function` capturing a `unique_ptr` cannot be enqueued #386

Closed dpuyda closed 4 months ago

dpuyda commented 4 months ago

The following code snippet does not compile:

  moodycamel::BlockingConcurrentQueue<std::function<void()>> queue;
  auto foo = std::make_unique<int>();
  queue.enqueue([foo = std::move(foo)] {});
dpuyda commented 4 months ago

Note that it also doesn't compile with an ordinary std::queue:

  std::queue<std::function<void()>> queue;
  auto foo = std::make_unique<int>();
  queue.emplace([foo = std::move(foo)] {});

A problem here is that std::function requires the argument to be copy-constructible. Found a few explanations: