Namespaces
Variants
Views
Actions

std::reference_wrapper

From cppreference.com
 
 
 
Function objects
Operator wrappers
Negators
Bind
bind(C++11)
is_bind_expression(C++11)
is_placeholder(C++11)
_1, _2, _3, ...(C++11)
Function wrappers
mem_fn(C++11)
function(C++11)
bad_function_call(C++11)
Reference wrappers
reference_wrapper(C++11)
ref
cref
(C++11)
(C++11)
Deprecated binders and adaptors
unary_function(deprecated)
binary_function(deprecated)
ptr_fun(deprecated)
pointer_to_unary_function(deprecated)
pointer_to_binary_function(deprecated)
mem_fun(deprecated)
mem_fun_t
mem_fun1_t
const_mem_fun_t
const_mem_fun1_t
(deprecated)
(deprecated)
(deprecated)
(deprecated)
mem_fun_ref(deprecated)
mem_fun_ref_t
mem_fun1_ref_t
const_mem_fun_ref_t
const_mem_fun1_ref_t
(deprecated)
(deprecated)
(deprecated)
(deprecated)
binder1st
binder2nd
(deprecated)
(deprecated)
bind1st
bind2nd
(deprecated)
(deprecated)
 
 
Defined in header <functional>
template< class T >
class reference_wrapper;
(since C++11)

Class template 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 (can be copied or stored in containers), but have reference semantics: calling a member function on a reference_wrapper invokes the member function of the underlying object.

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.

Contents

[edit] 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 T is a pointer to member function of class T0 that takes no arguments, then argument_type is T0*, possibly cv-qualified
3) if T is a class type with a member type T::argument_type, then argument_type is an alias of that

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 T is a pointer to member function of class T0 that takes one argument, then first_argument_type is T0*, possibly cv-qualified
3) if T is a class type with a member type T::first_argument_type, then first_argument_type is an alias of that

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 T is a pointer to member function of class T0 that takes one argument A1, then second_argument_type is A1, possibly cv-qualified
3) if T is a class type with a member type T::second_argument_type, then first_argument_type is an alias of that

[edit] Member functions

stores a reference in a new std::reference_wrapper object
(public member function) [edit]
rebinds a std::reference_wrapper
(public member function) [edit]
accesses the stored reference
(public member function) [edit]
calls the stored function
(public member function) [edit]

[edit] 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::vector<std::reference_wrapper<int>> v2(v.begin(), v.end());
    std::partition(v2.begin(), v2.end(), [](int n){return n<0;});
 
    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 << "Shuffled elements, partitioned: ";
    for(int i: v2) {
        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 
Shuffled elements, partitioned: -3 -1 -2 -4 4 1 3 0 2

[edit] See also

(C++11)
(C++11)
creates a reference_wrapper of type, deduced from its argument
(function template) [edit]
(C++11)
binds one or more arguments to a function object
(function template) [edit]