public member function
<future>

std::packaged_task::operator()

void operator()(Args... args);
Call stored task
Calls the stored task, forwarding args as its arguments.

- If the call to the stored task completes successfully, the value it returns (if any) is stored in the shared state.
- If the call to the stored task throws an exception, the exception is caught and also stored in the shared state.

In both cases, the shared state is made ready (which unblocks any threads currently waiting for it).

The shared state can be accessed by calling get on a future object returned by a previous call to member get_future.

The stored task is generally specified on construction. The effects of calling it depend on its type:

  • If the stored task is a function pointer or a function object, it is called forwarding the arguments to the call.
  • If the stored task is a pointer to a non-static member function, it is called using the first argument as the object on which the member is called (this may either be an object, a reference, or a pointer to it), and the remaining arguments are forwarded as arguments for the member function.
  • If it is a pointer to a non-static data member, it should be called with a single argument, and the function stores in the shared state a reference to that member of its argument (the argument may either be an object, a reference, or a pointer to it).

Parameters

args...
Arguments for the call.
If the type of the stored task is a member pointer, the first argument shall be an object for which that member is defined (or a reference, or a pointer to it).

Args... are the packaged_task template parameters, which represent the types of the arguments for the stored task .

Return value

none.

The return value of the stored task (if any) is stored in the shared state.

Data races

The packaged_task is modified.
The shared state is modified as an atomic operation (causing no data races).

Exception safety

Basic guarantee: if an exception is thrown, the packaged_task is in a valid state.

This member function throws an exception on the following conditions:
exception typeerror conditiondescription
future_errorfuture_errc::no_stateThe object has no shared state (it is a default-constructed packaged_task)
future_errorfuture_errc::promise_already_satisfiedThe stored task has already been called
This member function also throws if any copy or movement of the arguments throws and -depending on the library implementation- may also throw to report other situations.

See also