Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/functional/reference wrapper"

From cppreference.com
< cpp‎ | utility‎ | functional
m
(Member types: +)
Line 29: Line 29:
 
{{dsc hitem | type | definition }}
 
{{dsc hitem | type | definition }}
 
{{dsc | {{tt|type}} | {{tt|T}}}}
 
{{dsc | {{tt|type}} | {{tt|T}}}}
{{dsc | {{tt|result_type}}{{mark|deprecated in C++17}} | The return type of {{tt|T}} if {{tt|T}} is a function. Otherwise, not defined }}
+
{{dsc | {{tt|result_type}}{{mark|deprecated in C++17}}{{mark until c++20|removed=yes}} | The return type of {{tt|T}} if {{tt|T}} is a function. Otherwise, not defined }}
{{dsc | {{tt|argument_type}}{{mark|deprecated in C++17}} | 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>
+
{{dsc | {{tt|argument_type}}{{mark|deprecated in C++17}}{{mark until c++20|removed=yes}} | 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}}
{{dsc | {{tt|first_argument_type}}{{mark|deprecated in C++17}} | 1) if {{tt|T}} is a function or pointer to function that takes two arguments of types {{tt|A1}} and {{tt|A2}}, then {{tt|first_argument_type}} is {{tt|A1}}.<br>
+
{{dsc | {{tt|first_argument_type}}{{mark|deprecated in C++17}}{{mark until c++20|removed=yes}} | 1) if {{tt|T}} is a function or pointer to function that takes two arguments of types {{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
 
}}
 
}}
{{dsc | {{tt|second_argument_type}}{{mark|deprecated in C++17}} |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>
+
{{dsc | {{tt|second_argument_type}}{{mark|deprecated in C++17}}{{mark until c++20|removed=yes}} |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}}

Revision as of 11:43, 9 June 2018

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Function objects
Function invocation
(C++17)(C++23)
Identity function object
(C++20)
Reference wrappers
reference_wrapper
(C++11)
(C++11)(C++11)
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) which cannot 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.

If the stored reference is Template:concept, std::reference_wrapper is callable with the same arguments.

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.

std::reference_wrapper is guaranteed to be Template:concept.

(since C++17)

Contents

Member types

type definition
type T
result_type(deprecated in C++17)(removed in C++20) The return type of T if T is a function. Otherwise, not defined
argument_type(deprecated in C++17)(removed in C++20) 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(deprecated in C++17)(removed in C++20) 1) if T is a function or pointer to function that takes two arguments of types 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(deprecated in C++17)(removed in C++20) 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]

Deduction guides(since C++20)

Possible implementation

template <class T>
class reference_wrapper {
public:
  // types
  typedef T type;
 
  // construct/copy/destroy
  reference_wrapper(T& ref) noexcept : _ptr(std::addressof(ref)) {}
  reference_wrapper(T&&) = delete;
  reference_wrapper(const reference_wrapper&) noexcept = default;
 
  // assignment
  reference_wrapper& operator=(const reference_wrapper& x) noexcept = default;
 
  // access
  operator T& () const noexcept { return *_ptr; }
  T& get() const noexcept { return *_ptr; }
 
  template< class... ArgTypes >
  std::invoke_result_t<T&, ArgTypes...>
    operator() ( ArgTypes&&... args ) const {
    return std::invoke(get(), std::forward<ArgTypes>(args)...);
  }
 
private:
  T* _ptr;
};
 
// deduction guides
template<class T>
reference_wrapper(reference_wrapper<T>) -> reference_wrapper<T>;

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 <numeric>
#include <random>
#include <functional>
 
int main()
{
    std::list<int> l(10);
 
    std::iota(l.begin(), l.end(), -4);
    std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
 
    // can't use shuffle on a list (requires random access), but can use it on a vector
    std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
 
    std::cout << "Contents of the list: ";
    for (int n : l){ 
        std::cout << n << ' ';
    }
 
    std::cout << "\nContents of the list, as seen through a shuffled vector: ";
    for (int i : v){
        std::cout << i << ' ';
    }
 
    std::cout << "\n\nDoubling the values in the initial list...\n\n";
    for (int& i : l) {
        i *= 2;
    }
 
    std::cout << "Contents of the list, as seen through a shuffled vector: ";
    for (int i : v){
       std::cout << i << ' ';
    }
}

Possible output:

Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5 
Contents of the list, as seen through a shuffled vector: -1 2 -2 1 5 0 3 -3 -4 4 
Doubling the values in the initial list...
Contents of the list, as seen through a shuffled vector: -2 4 -4 2 10 0 6 -6 -8 8

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]