public member function
<future>

std::promise::promise

default (1)
promise();
with allocator (2)
template <class Alloc> promise (allocator_arg_t aa, const Alloc& alloc);
copy [deleted] (3)
promise (const promise&) = delete;
move (4)
promise (promise&& x) noexcept;
Construct promise
Constructs a promise object:

(1) default constructor
The object is initialized with access to a new empty shared state.
(2) constructor with allocator
Same as (1), but uses alloc to allocate memory for the shared state.
(3) copy constructor [deleted]
promise objects cannot be copied.
(4) move constructor
The constructed object acquires the shared state of x (if any).
x is left with no shared state.

Parameters

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 the shared state.
x
Another promise object of the same type (with the same template parameter, T).

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
25
26
// promise constructors
#include <iostream>       // std::cout
#include <functional>     // std::ref
#include <memory>         // std::allocator, std::allocator_arg
#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> foo;
  std::promise<int> bar = std::promise<int>(std::allocator_arg,std::allocator<int>());

  std::future<int> fut = bar.get_future();

  std::thread th (print_int, std::ref(fut));

  bar.set_value (20);

  th.join();
  return 0;
}


Output

value: 20

Data races

The move constructor (4) modifies x.

Exception safety

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).
The move constructor (4) never throws exceptions (no-throw guarantee).

See also