Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
We recommend using Visual Studio 2017

reference_wrapper Class

 

The latest version of this topic can be found at reference_wrapper Class.

Wraps a reference.

template <class Ty>  
class reference_wrapper  
 : public unary_function<T1, Ret>        // see below  
 : public binary_function<T1, T2, Ret>   // see below  
 {  
public:  
    typedef Ty type;  
    typedef T0 result_type; // see below  
 
    reference_wrapper(Ty&);

 
    Ty& get() const;

 operator Ty&() const;

 
    template <class T1, class T2, ..., class TN>  
typename result_of<T(T1, T2, ..., TN)>::type operator()(T1&, T2&, ..., TN&);

 
private:  
    Ty *ptr; // exposition only  
 };  

A reference_wrapper<Ty> is copy constructible and assignable, and holds a pointer that points to an object of type Ty.

A specialization reference_wrapper<Ty> is derived from std::unary_function<T1, Ret> (hence defining the nested type result_type as a synonym for Ret and the nested type argument_type as a synonym for T1) only if the type Ty is:

a function type or pointer to function type taking one argument of type T1 and returning Ret; or

a pointer to a member function Ret T::f() cv, where cv represents the member function's cv-qualifiers; the type T1 is cv``T*; or

a class type that is derived from unary_function<T1, Ret>.

A specialization reference_wrapper<Ty> is derived from std::binary_function<T1, T2, Ret> (hence defining the nested type result_type as a synonym for Ret, the nested type first_argument_type as a synonym for T1, and the nested type second_argument_type as a synonym for T2) only if the type Ty is:

a function type or pointer to function type taking two arguments of types T1 and T2 and returning Ret; or

a pointer to a member function Ret T::f(T2) cv, where cv represents the member function's cv-qualifiers; the type T1 is cv``T*; or

a class type that is derived from binary_function<T1, T2, Ret>.

Constructors

reference_wrapper::reference_wrapperConstructs a reference_wrapper.

Typedefs

reference_wrapper::result_typeThe weak result type of the wrapped reference.
reference_wrapper::typeThe type of the wrapped reference.

Member Functions

reference_wrapper::getObtains the wrapped reference.

Operators

reference_wrapper::operator Ty&Gets a pointer to the wrapped reference.
reference_wrapper::operator()Calls the wrapped reference.

Header: <functional>

Namespace: std

Obtains the wrapped reference.

Ty& get() const;

Remarks

The member function returns INVOKE(get(), t1, t2, ..., tN).

Example

// std_tr1__functional__reference_wrapper_get.cpp   
// compile with: /EHsc   
#include <functional>   
#include <iostream>   
  
int main()   
    {   
    int i = 1;   
    std::reference_wrapper<int> rwi(i);   
  
    std::cout << "i = " << i << std::endl;   
    std::cout << "rwi = " << rwi << std::endl;   
    rwi.get() = -1;   
    std::cout << "i = " << i << std::endl;   
  
    return (0);   
    }  
  

i = 1  
rwi = 1  
i = -1  

Gets a pointer to the wrapped reference.

operator Ty&() const;

Parameters

Remarks

The member operator returns *ptr.

Example

// std_tr1__functional__reference_wrapper_operator_cast.cpp   
// compile with: /EHsc   
#include <functional>   
#include <iostream>   
  
int main()   
    {   
    int i = 1;   
    std::reference_wrapper<int> rwi(i);   
  
    std::cout << "i = " << i << std::endl;   
    std::cout << "(int)rwi = " << (int)rwi << std::endl;   
  
    return (0);   
    }  
  

i = 1  
(int)rwi = 1  

Calls the wrapped reference.

template <class T1, class T2, ..., class TN>  
typename result_of<T(T1, T2, ..., TN)>::type operator()(T1& t1, T2& t2, ..., TN& tN);

Parameters

TN
The type of the Nth call argument.

tN
The Nth call argument.

Remarks

The template member operator returns INVOKE(get(), t1, t2, ..., tN).

Example

// std_tr1__functional__reference_wrapper_operator_call.cpp   
// compile with: /EHsc   
#include <functional>   
#include <iostream>   
  
int neg(int val)   
    {   
    return (-val);   
    }   
  
int main()   
    {   
    std::reference_wrapper<int (int)> rwi(neg);   
  
    std::cout << "rwi(3) = " << rwi(3) << std::endl;   
  
    return (0);   
    }  
  

rwi(3) = -3  

Constructs a reference_wrapper.

explicit reference_wrapper(Ty& val);

Parameters

Ty
The type to wrap.

val
The value to wrap.

Remarks

The constructor sets the stored value ptr to &val.

Example

// std_tr1__functional__reference_wrapper_reference_wrapper.cpp   
// compile with: /EHsc   
#include <functional>   
#include <iostream>   
  
int neg(int val)   
    {   
    return (-val);   
    }   
  
int main()   
    {   
    int i = 1;   
    std::reference_wrapper<int> rwi(i);   
  
    std::cout << "i = " << i << std::endl;   
    std::cout << "rwi = " << rwi << std::endl;   
    rwi.get() = -1;   
    std::cout << "i = " << i << std::endl;   
  
    return (0);   
    }  
  

i = 1  
rwi = 1  
i = -1  

The weak result type of the wrapped reference.

typedef T0 result_type;  

Remarks

The typedef is a synonym for the weak result type of a wrapped function.

Example

// std_tr1__functional__reference_wrapper_result_type.cpp   
// compile with: /EHsc   
#include <functional>   
#include <iostream>   
  
int neg(int val)   
    {   
    return (-val);   
    }   
  
int main()   
    {   
    typedef std::reference_wrapper<int (int)> Mywrapper;   
    Mywrapper rwi(neg);   
    Mywrapper::result_type val = rwi(3);   
  
    std::cout << "val = " << val << std::endl;   
  
    return (0);   
    }  
  

val = -3  

The type of the wrapped reference.

typedef Ty type;  

Remarks

The typedef is a synonym for the template argument Ty.

Example

// std_tr1__functional__reference_wrapper_type.cpp   
// compile with: /EHsc   
#include <functional>   
#include <iostream>   
  
int neg(int val)   
    {   
    return (-val);   
    }   
  
int main()   
    {   
    int i = 1;   
    typedef std::reference_wrapper<int> Mywrapper;   
    Mywrapper rwi(i);   
    Mywrapper::type val = rwi.get();   
  
    std::cout << "i = " << i << std::endl;   
    std::cout << "rwi = " << val << std::endl;   
  
    return (0);   
    }  
  

i = 1  
rwi = 1  

cref Function
ref Function

Show:
© 2018 Microsoft