public member function
<future>

std::packaged_task::packaged_task

默认 (1)
packaged_task() noexcept;
initialization (2)
template <class Fn>  explicit packaged_task (Fn&& fn);
with allocator (3)
template <class Fn, class Alloc>  explicit packaged_task (allocator_arg_t aa, const Alloc& alloc, Fn&& fn);
copy [deleted] (4)
packaged_task (packaged_task&) = delete;
move (5)
packaged_task (packaged_task&& x) noexcept;
默认 (1)
packaged_task() noexcept;
initialization (2)
template <class Fn>  explicit packaged_task (Fn&& fn);
with allocator (3)
template <class Fn, class Alloc>  explicit packaged_task (allocator_arg_t aa, const Alloc& alloc, Fn&& fn);
copy [deleted] (4)
packaged_task (const packaged_task&) = delete;
move (5)
packaged_task (packaged_task&& x) noexcept;
Construct packaged task
Constructs a packaged_task

(1) 默认构造函数
The object is initialized with no shared state and no stored task.
(2) initialization constructor
The object has a shared state, and its stored task is initialized to fn.
(3) initialization constructor with allocator
Same as (2), but uses alloc to allocate memory for the internal data structures.
(4) copy constructor [deleted]
packaged_task objects cannot be copied (deleted constructor).
(5) move constructor
The constructed object acquires the shared state of x (if any), and its stored task is moved.
x is left with no shared state.

Versions (2) and (3) are never called if the decay type of Fn is the packaged_task.

参数

fn
A callable pointer to function, pointer to member, or any kind of move-constructible function object (i.e., an object whose class defines operator(), including closures and function objects).
The return type and arguments taken by this function shall correspond to the packaged_task's template parameters (Ret and Args...).
The stored task is set to fn (internally initialized with std::forward<Fn>(fn)).
aa
The std::allocator_arg value. This constant value is merely used to explicitly select this constructor overload.
alloc
Allocator object.
The container keeps and uses an internal copy of this allocator, and uses it to allocate storage for its internal data structures.
x
Another packaged_task object of the same type (with the same template parameters, Ret and Args).

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// packaged_task construction / assignment
#include <iostream>     // std::cout
#include <utility>      // std::move
#include <future>       // std::packaged_task, std::future
#include <thread>       // std::thread

int main ()
{
  std::packaged_task<int(int)> foo;                          // default-constructed
  std::packaged_task<int(int)> bar ([](int x){return x*2;}); // initialized

  foo = std::move(bar);                                      // move-assignment

  std::future<int> ret = foo.get_future();  // get future

  std::thread(std::move(foo),10).detach();  // spawn thread and call task

  // ...

  int value = ret.get();                    // wait for the task to finish and get result

  std::cout << "The double of 10 is " << value << ".\n";

  return 0;
}

输出

The double of 10 is 20.


数据竞争

The move constructor (5) modifies x.

异常安全

Strong guarantee: no effects in case an exception is thrown.
Depending on the library implementation, this member function may throw exceptions on certain conditions (such as bad_alloc on a failure to allocate memory).
Neither the default (1) nor the move constructor (5) throw exceptions (no-throw guarantee).

另见