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).

参数

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

返回值

*this

示例

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;
}

输出

value: 10
value: 20


数据竞争

Both the object and rhs are modified.

异常安全

无异常保证: 绝不抛出异常。

另见