public member function
<functional>
初始化 (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;
}
|
输出
数据竞争
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.