Skip to content
Tutorials Center
My learning

Topics

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".

C++
int x = 1;
std::reference_wrapper<int> r = x;
r.get() = 5;     // x is now 5

Try it

C++ References in a container

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.

C++ ref with bind, thread and make_pair

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

C++
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; makes w refer to other. To change the value, write w.get() = value. With a plain reference the same line would assign the value.
  • No implicit member access. w.size() does not compile; use w.get().size(). Implicit conversion happens only where a T& is expected, so templates that deduce the type may see the wrapper type instead.
  • Threads and async. std::thread and std::async decay-copy their arguments. Passing a variable to a T& parameter without std::ref does 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::ranges projections and comparators accept wrappers through implicit conversion in many cases, as the example shows.
  • Deleted for temporaries. std::ref(5) and std::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 to T&, so write explicit lambdas when sorting.
  • Version: reference_wrapper is C++11; it is constexpr and supports incomplete types since C++20.