Namespaces
Variants

cpp/utility/functional/reference wrapper: Difference between revisions

From cppreference.com
emphasize primary usage
P12bot (talk | contribs)
m Shorten template names. Use {{lc}} where appropriate.
Line 1: Line 1:
{{cpp/title|reference_wrapper}}
{{cpp/title|reference_wrapper}}
{{cpp/utility/functional/reference_wrapper/navbar}}
{{cpp/utility/functional/reference_wrapper/navbar}}
{{ddcl list begin}}
{{begin}}
{{ddcl list header | functional }}
{{header | functional }}
{{ddcl list item | notes={{mark since c++11}} |
{{| notes={{mark since c++11}} |
template< class T >
template< class T >
class reference_wrapper;
class reference_wrapper;
}}
}}
{{ddcl list end}}
{{end}}


{{tt|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 {{c|std::vector}} or {{c|std::pair}}) which can not normally hold references.
{{tt|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}} or {{|std::pair}}) which can not normally hold references.


Specifically, {{tt|std::reference_wrapper}} is a {{concept|CopyConstructible}} and {{concept|CopyAssignable}} wrapper around a reference to object or reference to function of type {{tt|T}}. Instances of {{tt|std::reference_wrapper}} are objects (they can be copied or stored in containers) but they are implicitly convertible to {{c|T&}}, so that they can be used as arguments with the functions that take the underlying type by reference.
Specifically, {{tt|std::reference_wrapper}} is a {{concept|CopyConstructible}} and {{concept|CopyAssignable}} wrapper around a reference to object or reference to function of type {{tt|T}}. Instances of {{tt|std::reference_wrapper}} are objects (they can be copied or stored in containers) but they are implicitly convertible to {{c|T&}}, so that they can be used as arguments with the functions that take the underlying type by reference.


Helper functions {{c|std::ref}} and {{c|std::cref}} are often used to generate {{tt|std::reference_wrapper}} objects.  
Helper functions {{|std::ref}} and {{|std::cref}} are often used to generate {{tt|std::reference_wrapper}} objects.  


{{tt|std::reference_wrapper}} is also used to pass objects to {{c|std::bind}} or to the constructor of {{c|std::thread}} by reference.
{{tt|std::reference_wrapper}} is also used to pass objects to {{|std::bind}} or to the constructor of {{|std::thread}} by reference.


===Member types===
===Member types===
{{dcl list begin}}
{{begin}}
{{dcl list hitem | type | definition }}
{{hitem | type | definition }}
{{dcl list item | {{tt|type}} | {{tt|T}}}}
{{| {{tt|type}} | {{tt|T}}}}
{{dcl list item | {{tt|result_type}} | The return type of {{tt|T}} if {{tt|T}} is a function. Otherwise, not defined }}
{{| {{tt|result_type}} | The return type of {{tt|T}} if {{tt|T}} is a function. Otherwise, not defined }}
{{dcl list item | {{tt|argument_type}} | 1) if {{tt|T}} is a function or pointer to function that takes one argument of type {{tt|A1}},  then {{tt|argument_type}} is {{tt|A1}}.<br>
{{| {{tt|argument_type}} | 1) if {{tt|T}} is a function or pointer to function that takes one argument of type {{tt|A1}},  then {{tt|argument_type}} is {{tt|A1}}.<br>
2) if {{tt|T}} is a pointer to member function of class {{tt|T0}} that takes no arguments, then {{tt|argument_type}} is {{tt|T0*}}, possibly cv-qualified<br>
2) if {{tt|T}} is a pointer to member function of class {{tt|T0}} that takes no arguments, then {{tt|argument_type}} is {{tt|T0*}}, possibly cv-qualified<br>
3) if {{tt|T}} is a class type with a member type {{tt|T::argument_type}}, then {{tt|argument_type}} is an alias of that}}
3) if {{tt|T}} is a class type with a member type {{tt|T::argument_type}}, then {{tt|argument_type}} is an alias of that}}
{{dcl list item | {{tt|first_argument_type}} | 1) if {{tt|T}} is a function or pointer to function that takes two arguments of type s {{tt|A1}} and {{tt|A2}}, then {{tt|first_argument_type}} is {{tt|A1}}.<br>
{{| {{tt|first_argument_type}} | 1) if {{tt|T}} is a function or pointer to function that takes two arguments of type s {{tt|A1}} and {{tt|A2}}, then {{tt|first_argument_type}} is {{tt|A1}}.<br>
2) if {{tt|T}} is a pointer to member function of class {{tt|T0}} that takes one argument, then {{tt|first_argument_type}} is {{tt|T0*}}, possibly cv-qualified<br>
2) if {{tt|T}} is a pointer to member function of class {{tt|T0}} that takes one argument, then {{tt|first_argument_type}} is {{tt|T0*}}, possibly cv-qualified<br>
3) if {{tt|T}} is a class type with a member type {{tt|T::first_argument_type}}, then {{tt|first_argument_type}} is an alias of that
3) if {{tt|T}} is a class type with a member type {{tt|T::first_argument_type}}, then {{tt|first_argument_type}} is an alias of that
}}
}}
{{dcl list item | {{tt|second_argument_type}} |1) if {{tt|T}} is a function or pointer to function that takes two arguments of type s {{tt|A1}} and {{tt|A2}}, then {{tt|second_argument_type}} is {{tt|A2}}.<br>
{{| {{tt|second_argument_type}} |1) if {{tt|T}} is a function or pointer to function that takes two arguments of type s {{tt|A1}} and {{tt|A2}}, then {{tt|second_argument_type}} is {{tt|A2}}.<br>
2) if {{tt|T}} is a pointer to member function of class {{tt|T0}} that takes one argument {{tt|A1}}, then {{tt|second_argument_type}} is {{tt|A1}}, possibly cv-qualified<br>
2) if {{tt|T}} is a pointer to member function of class {{tt|T0}} that takes one argument {{tt|A1}}, then {{tt|second_argument_type}} is {{tt|A1}}, possibly cv-qualified<br>
3) if {{tt|T}} is a class type with a member type {{tt|T::second_argument_type}}, then {{tt|second_argument_type}} is an alias of that}}
3) if {{tt|T}} is a class type with a member type {{tt|T::second_argument_type}}, then {{tt|second_argument_type}} is an alias of that}}
{{dcl list end}}
{{end}}


===Member functions===
===Member functions===
{{dcl list begin}}
{{begin}}
{{dcl list template | cpp/utility/functional/reference_wrapper/dcl list constructor}}
{{| cpp/utility/functional/reference_wrapper/dcl list constructor}}
{{dcl list template | cpp/utility/functional/reference_wrapper/dcl list operator{{=}} }}
{{| cpp/utility/functional/reference_wrapper/dcl list operator{{=}} }}
{{dcl list template | cpp/utility/functional/reference_wrapper/dcl list get}}
{{| cpp/utility/functional/reference_wrapper/dcl list get}}
{{dcl list template | cpp/utility/functional/reference_wrapper/dcl list operator()}}
{{| cpp/utility/functional/reference_wrapper/dcl list operator()}}
{{dcl list end}}
{{end}}


===Example===
===Example===
Line 87: Line 87:


===See also===
===See also===
{{dcl list begin}}
{{begin}}
{{dcl list template | cpp/utility/functional/dcl list ref}}
{{| cpp/utility/functional/dcl list ref}}
{{dcl list template | cpp/utility/functional/dcl list bind}}
{{| cpp/utility/functional/dcl list bind}}
{{dcl list end}}
{{end}}


[[de:cpp/utility/functional/reference wrapper]]
[[de:cpp/utility/functional/reference wrapper]]

Revision as of 04:15, 1 June 2013

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Function objects
Function invocation
(C++17)(C++23)
Identity function object
(C++20)
Transparent operator wrappers
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

Old binders and adaptors
(until C++17*)
(until C++17*)
(until C++17*)
(until C++17*)  
(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
(until C++17*)(until C++17*)
(until C++17*)(until C++17*)

(until C++17*)
(until C++17*)(until C++17*)(until C++17*)(until C++17*)
(until C++20*)
(until C++20*)
 
 
Defined in header <functional>
template< class T >
class reference_wrapper;
(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 or std::pair) which can not normally hold references.

Specifically, std::reference_wrapper is a Template:concept and Template:concept 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.

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.

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 second_argument_type is an alias of that

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]

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

See also

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