On this page
std::reference_wrapper
Defined in header <functional> |
||
---|---|---|
|
(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
) which cannot normally hold references.
Specifically, std::reference_wrapper
is a CopyConstructible and CopyAssignable 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.
If the stored reference is Callable, std::reference_wrapper
is callable with the same arguments.
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 by reference to std::bind
, the constructor of std::thread
, or the helper functions std::make_pair
and std::make_tuple
.
|
(since C++17) |
|
(since C++20) |
Member types
type | definition |
---|---|
type |
T |
result_type (deprecated in C++17)(removed in C++20) |
The return type of T if T is a function. Otherwise, not defined |
argument_type (deprecated in C++17)(removed in C++20) |
|
first_argument_type (deprecated in C++17)(removed in C++20) |
|
second_argument_type (deprecated in C++17)(removed in C++20) |
|
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) |
Deduction guides(since C++17)
Helper classes
(C++23)
|
determines the common reference type of reference_wrapper and non-reference_wrapper (class template specialization) |
Possible implementation
|
Example
Demonstrates the use of std::reference_wrapper
as a container of references, which makes it possible to access the same container using multiple indexes.
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <random>
#include <vector>
void print(auto const rem, std::ranges::range auto const& v)
{
for (std::cout << rem; auto const& e : v)
std::cout << e << ' ';
std::cout << '\n';
}
int main()
{
std::list<int> l(10);
std::iota(l.begin(), l.end(), -4);
// can't use shuffle on a list (requires random access), but can use it on a vector
std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
std::ranges::shuffle(v, std::mt19937{std::random_device{}()});
print("Contents of the list: ", l);
print("Contents of the list, as seen through a shuffled vector: ", v);
std::cout << "Doubling the values in the initial list...\n";
std::ranges::for_each(l, [](int& i) { i *= 2; });
print("Contents of the list, as seen through a shuffled vector: ", v);
}
Possible output:
Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5
Contents of the list, as seen through a shuffled vector: -1 2 -2 1 5 0 3 -3 -4 4
Doubling the values in the initial list...
Contents of the list, as seen through a shuffled vector: -2 4 -4 2 10 0 6 -6 -8 8
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) |
(C++20)(C++20)
|
get the reference type wrapped in std::reference_wrapper (class template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper