public member function
<future>

std::promise::get_future

future<T> get_future();
Get future
Returns a future object associated with the object's shared state.

The future object returned can access the value or exception set on the shared state by the promise object once this is ready.

Only one future object can be retrieved for each promise shared state.

After this function has been called, the promise is expected to make its shared state ready at some point (by setting a value or an exception), otherwise it is automatically made ready on destruction containing an exception of type future_error (with a broken_promise error condition).

Parameters

none

Return value

A future object referring to the same shared state as this promise.
T is the type of the value (the template parameter of promise).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// promise example
#include <iostream>       // std::cout
#include <functional>     // std::ref
#include <thread>         // std::thread
#include <future>         // std::promise, std::future

void print_int (std::future<int>& fut) {
  int x = fut.get();
  std::cout << "value: " << x << '\n';
}

int main ()
{
  std::promise<int> prom;                      // create promise

  std::future<int> fut = prom.get_future();    // engagement with future

  std::thread th1 (print_int, std::ref(fut));  // send future to new thread

  prom.set_value (10);                         // fulfill promise
                                               // (synchronizes with getting the future)
  th1.join();
  return 0;
}

Output

value: 10


Data races

The promise object is modified.

Exception safety

Basic guarantee: if an exception is thrown, the promise object 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 was moved-from)
future_errorfuture_errc::future_already_retrievedA previous call to this member function already retrieved a future
Depending on the library implementation, this member function may also throw exceptions to report other situations (such as bad_alloc or system_error).

See also