Showing content from https://cplusplus.com/reference/functional/reference_wrapper/reference_wrapper/ below:
public member function
<functional>
std::reference_wrapper::reference_wrapper initialization (1)
reference_wrapper (type& ref) noexcept;reference_wrapper (type&&) = delete;
copy (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()
).
Parameters
-
ref
-
An lvalue reference, whose reference is stored in the object.
type is a member type describing the referred type (it is an alias of the class template parameter, T).
-
x
-
A reference_wrapper object of the same type (i.e., with the same template parameter, T).
Example
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;
}
Output:
Data races 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.
Exception safetyNo-throw guarantee: never throws exceptions.
See also
-
ref
-
Construct reference_wrapper (function template)
-
cref
-
Construct reference_wrapper to const (function template)
-
reference_wrapper::operator=
-
Copy assignment (public member function)
-
reference_wrapper::get
-
Access element (public member function)
RetroSearch is an open source project built by @garambo
| Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4