public member function
<functional>

std::reference_wrapper::reference_wrapper

初始化 (1)
reference_wrapper (type& ref) noexcept;reference_wrapper (type&&) = delete;
複製 (2)
reference_wrapper (const reference_wrapper& x) noexcept;
Construct reference wrapper
Constructs a reference_wrapper object

(1) initialization
The object stores a reference to ref.
Note that this constructor only accepts lvalues (the rvalue version is deleted).
(2) copy
The object stores a reference to the same object or function x does (x.get()).

引數

ref
An lvalue reference, whose reference is stored in the object.
type 是一個描述被引用型別的成員型別(它是類模板引數 T 的別名)。
x
A reference_wrapper object of the same type (i.e., with the same template parameter, T).

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// reference_wrapper example:
#include <iostream>     // std::cout
#include <functional>   // std::reference_wrapper

int main () {
  int a(10),b(20),c(30);

  // an array of "references":
  std::reference_wrapper<int> refs[] = {a,b,c};

  std::cout << "refs:";
  for (int& x : refs) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

輸出
refs: 10 20 30


資料競爭

The initialization constructor (1) does not access ref, but it acquires a reference to it, which can be used to access or modify it.
The copy constructor (2) accesses its argument (x), acquiring a reference to its referred element, which can be used to access or modify it.

異常安全

無異常保證: 絕不丟擲異常。

另見