class template
<functional>

std::reference_wrapper

template <class T> class reference_wrapper;
Reference wrapper
Class that emulates a reference to an element of type T, but which can be copied (it is both copy-constructible and copy-assignable).

Template parameters

T
Type of the referred element.
This can either be an object or a function.

Member types

member typedefinition
typeThe template parameter (T)
result_typeIf T is a pointer to function or to member function type: the return type of T.
If T is a class, and has a result_type member: T::result_type.
Otherwise, not defined.
argument_typeIf T is a function type or a pointer to function type taking a single argument: the type of the argument taken by T.
If T is a pointer to member function: the class type of which T is a member (with the same const/volatile qualification as the member function).
If T is a class, and has an argument_type member: T::argument_type.
Otherwise, not defined.
first_argument_typeIf T is a function type or a pointer to function type taking two arguments: the type of the first argument taken by T.
If T is a pointer to member function taking a single argument: the class type of which T is a member (with the same const/volatile qualification as the member function).
If T is a class, and has a first_argument_type member: T::first_argument_type.
Otherwise, not defined.
second_argument_typeIf T is a function type or a pointer to function type taking two arguments: The type of the second argument taken by T.
If T is a pointer to member function taking a single argument: the argument taken by T.
If T is a class, and has a second_argument_type member: T::second_argument_type.
Otherwise, not defined.
Note that, except for type, the other member types are not always defined.

Member functions


Non-member helper functions


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;
}


Possible output:
refs: 10 20 30