Reference

public member function
<functional>

std::reference_wrapper::operator=

reference_wrapper& operator= (const reference_wrapper& rhs) noexcept;
Copy assignment
Copies the reference held by rhs, replacing the reference currently held in *this.

After the call, both rhs and *this refer to the same object or function.

Parameters

rhs
A reference_wrapper object of the same type (i.e., 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
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;
}

Output:
a: 10
b: 22
a: 21
b: 21


Data races

The object is modified.
The object acquires a reference to the element referred by rhs (rhs.get() ), which can be used to access or modify it.

Exception safety

No-throw guarantee: this member function never throws exceptions.

See also

ref
Construct reference_wrapper (function template)
cref
Construct reference_wrapper to const (function template)
reference_wrapper::reference_wrapper
Construct reference wrapper (public member function)
reference_wrapper::get
Access element (public member function)

AltStyle によって変換されたページ (->オリジナル) /