cpp/utility/functional/reference wrapper: Difference between revisions
Improved the example code |
minor format spacing |
||
| Line 60: | Line 60: | ||
std::cout << "Contents of the list: "; | std::cout << "Contents of the list: "; | ||
for(int n: l) { | for (int n : l) { | ||
std::cout << n << ' '; | std::cout << n << ' '; | ||
} | } | ||
| Line 66: | Line 66: | ||
std::cout << "Contents of the list, shuffled: "; | std::cout << "Contents of the list, shuffled: "; | ||
for(int i: v) { | for (int i : v) { | ||
std::cout << i << ' '; | std::cout << i << ' '; | ||
} | } | ||
| Line 72: | Line 72: | ||
std::cout << "Doubling the values in the initial list...\n"; | std::cout << "Doubling the values in the initial list...\n"; | ||
for(int & i: l) { | for (int &i : l) { | ||
i *= 2; | i *= 2; | ||
} | } | ||
std::cout << "Shuffled vector actually contains references: "; | std::cout << "Shuffled vector actually contains references: "; | ||
for(int i: v) { | for (int i : v) { | ||
std::cout << i << ' '; | std::cout << i << ' '; | ||
} | } | ||
Revision as of 15:12, 12 May 2014
| Defined in header <functional>
|
||
template< class T >
class reference_wrapper;
|
(since C++11) | |
std::reference_wrapper is a class template that wraps a reference in a copyable, assignable object. It is frequently used as a mechanism to store references inside standard containers (like std::vector or std::pair) which can not normally hold references.
Specifically, std::reference_wrapper is a Template:concept and Template:concept wrapper around a reference to object or reference to function of type T. Instances of std::reference_wrapper are objects (they can be copied or stored in containers) but they are implicitly convertible to T&, so that they can be used as arguments with the functions that take the underlying type by reference.
Helper functions std::ref and std::cref are often used to generate std::reference_wrapper objects.
std::reference_wrapper is also used to pass objects to std::bind or to the constructor of std::thread by reference.
Member types
| type | definition |
type
|
T
|
result_type
|
The return type of T if T is a function. Otherwise, not defined
|
argument_type
|
1) if T is a function or pointer to function that takes one argument of type A1, then argument_type is A1.2) if |
first_argument_type
|
1) if T is a function or pointer to function that takes two arguments of type s A1 and A2, then first_argument_type is A1.2) if |
second_argument_type
|
1) if T is a function or pointer to function that takes two arguments of type s A1 and A2, then second_argument_type is A2.2) if |
Member functions
| stores a reference in a new std::reference_wrapper object (public member function) | |
| rebinds a std::reference_wrapper (public member function) | |
| accesses the stored reference (public member function) | |
| calls the stored function (public member function) |
Example
Demonstrates the use of reference_wrapper as a container of references, which makes it possible to access the same container using multiple indexes
#include <algorithm>
#include <list>
#include <vector>
#include <iostream>
#include <functional>
int main()
{
std::list<int> l = {-4, -3, -2, -1, 0, 1, 2, 3, 4};
std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
std::random_shuffle(v.begin(), v.end());
std::cout << "Contents of the list: ";
for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
std::cout << "Contents of the list, shuffled: ";
for (int i : v) {
std::cout << i << ' ';
}
std::cout << '\n';
std::cout << "Doubling the values in the initial list...\n";
for (int &i : l) {
i *= 2;
}
std::cout << "Shuffled vector actually contains references: ";
for (int i : v) {
std::cout << i << ' ';
}
std::cout << '\n';
}
Output:
Contents of the list: -4 -3 -2 -1 0 1 2 3 4
Contents of the list, shuffled: 0 -1 3 4 -4 1 -2 -3 2
Doubling the values in the initial list...
Shuffled vector actually contains references: 0 -2 6 8 -8 2 -4 -6 4
See also
(C++11)(C++11) |
creates a std::reference_wrapper with a type deduced from its argument (function template) |
(C++11) |
binds one or more arguments to a function object (function template) |