A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/future/promise/operator=/ below:

public member function

<future>

std::promise::operator= move (1)
promise& operator= (promise&& rhs) noexcept;
copy [deleted] (2)
promise& operator= (const promise&) = delete;

Move-assign promise

Acquires the shared state of rhs (if any).

The shared state associated to the object before the call (if any) is abandoned (as if the promise was destroyed).

rhs is left with no shared state: any operations that would affect its shared state will throw future_error with a no_state error condition.

promise objects cannot be copied (2).



Parameters
rhs
Another promise object of the same type (with the same template parameter T).

Return value*this
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
27
// promise::operator=
#include <iostream>       // std::cout
#include <thread>         // std::thread
#include <future>         // std::promise, std::future

std::promise<int> prom;

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

int main ()
{
  std::thread th1 (print_global_promise);
  prom.set_value (10);
  th1.join();

  prom = std::promise<int>();    // reset, by move-assigning a new promise

  std::thread th2 (print_global_promise);
  prom.set_value (20);
  th2.join();

  return 0;
}

Output


Data races Both the object and rhs are modified.

Exception safetyNo-throw guarantee: never throws exceptions.

See also
promise::set_value
Set value (public member function)
promise::promise
Construct promise (public member function)
promise::swap
Swap shared states (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4