public member function
<functional>

std::reference_wrapper::operator=

reference_wrapper& operator= (const reference_wrapper& rhs) noexcept;
複製賦值
rhs 持有的引用複製到 *this 中,替換當前持有的引用。

呼叫後,rhs*this 都引用同一個物件或函式。

引數

rhs
一個相同型別的 reference_wrapper 物件(即,具有相同的模板引數 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
28
29
30
31
32
33
// reference_wrapper::operator=
#include <iostream>     // std::cout
#include <functional>   // std::reference_wrapper

int main () {
  int a,b;

  // reference_wrapper assignment:
  a=10; b=20;
  std::reference_wrapper<int> wrap1 (a);
  std::reference_wrapper<int> wrap2 (b);

  wrap1 = wrap2;
  ++wrap1; ++wrap2;

  std::cout << "a: " << a << '\n';
  std::cout << "b: " << b << '\n';

  std::cout << '\n';

  // note the difference with:
  a=10; b=20;
  int& ref1 (a);
  int& ref2 (b);

  ref1 = ref2;
  ++ref1; ++ref2;

  std::cout << "a: " << a << '\n';
  std::cout << "b: " << b << '\n';

  return 0;
}

輸出
a: 10
b: 22

a: 21
b: 21


資料競爭

物件被修改。
該物件獲得對 rhs 所引用元素的引用(rhs.get()),可用於訪問或修改該元素。

異常安全

無異常保證:此成員函式從不丟擲異常。

另見