C++ Reference ยท Memory and Utilities
C++ std::reference_wrapper, std::ref and std::cref
Reference for std::reference_wrapper, std::ref and std::cref: store references in containers, pass them to bind and threads, and avoid dangling.
std::reference_wrapper<T> (header <functional>) is a small copyable, assignable object that holds a reference to a T. A plain C++ reference cannot be rebound, copied as a reference, or stored in a container. The wrapper can: it behaves like a pointer that is never null and converts implicitly back to T&.
You almost never write std::reference_wrapper<T> by hand. The helper functions std::ref(x) and std::cref(x) create wrappers for mutable and const references. They are the standard way to tell std::bind, std::thread, std::async and std::make_tuple-like utilities "pass this by reference, do not copy it".
int x = 1; std::reference_wrapper<int> r = x; r.get() = 5; // x is now 5
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
template<class T> class reference_wrapper; reference_wrapper<T> ref(T& t) noexcept; reference_wrapper<const T> cref(const T& t) noexcept; // Deleted: ref(T&&) and cref(const T&&), so temporaries cannot be wrapped.
| Member or function | Description |
|---|---|
reference_wrapper(T& x) |
Binds to x. Construction from an rvalue is deleted. |
operator= |
Rebinds the wrapper to the object of another wrapper (it does not assign the referenced values). |
get() / operator T&() |
Returns the referenced object. |
operator()(args...) |
If T is callable, calls it. Works with std::invoke. |
ref(x), cref(x) |
Create wrappers. ref(wrapper) returns the wrapper itself instead of nesting. |
Returns: a wrapper object; get() returns T&.
Errors / undefined behavior: using a wrapper after the referenced object is destroyed is undefined behavior, exactly like a dangling reference.
Usage notes
- Rebinding vs assigning.
w = other;makeswrefer toother. To change the value, writew.get() = value. With a plain reference the same line would assign the value. - No implicit member access.
w.size()does not compile; usew.get().size(). Implicit conversion happens only where aT&is expected, so templates that deduce the type may see the wrapper type instead. - Threads and async.
std::threadandstd::asyncdecay-copy their arguments. Passing a variable to aT¶meter withoutstd::refdoes not compile (or, with bind, silently modifies a copy). - Containers. Use it for "a vector of references" when pointers would be awkward; elements stay valid only while the referents do.
- Standard algorithms.
std::rangesprojections and comparators accept wrappers through implicit conversion in many cases, as the example shows. - Deleted for temporaries.
std::ref(5)andstd::ref(std::string("x"))are errors, which prevents the most common dangling mistake. - Hash and comparison: the wrapper itself has no
operator<or hash; comparisons work only through implicit conversion toT&, so write explicit lambdas when sorting. - Version:
reference_wrapperis C++11; it isconstexprand supports incomplete types since C++20.