public member function
<functional>
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 safety
No-throw guarantee: never throws exceptions.