Skip to content

Latest commit

 

History

History
318 lines (224 loc) · 6.01 KB

reference-wrapper-class.md

File metadata and controls

318 lines (224 loc) · 6.01 KB
descriptiontitlems.datef1_keywordshelpviewer_keywordsms.assetid
Learn more about: reference_wrapper Class
reference_wrapper Class
11/04/2016
functional/std::reference_wrapper
type_traits/std::reference_wrapper
xrefwrap/std::reference_wrapper
type_traits/std::reference_wrapper::get
type_traits/std::reference_wrapper::operator()
functional/std::reference_wrapper::result_type
functional/std::reference_wrapper::type
functional/std::reference_wrapper::get
functional/std::reference_wrapper::operator()
std::reference_wrapper [C++]
std::reference_wrapper [C++]
std::reference_wrapper [C++], result_type
std::reference_wrapper [C++], type
std::reference_wrapper [C++], get
90b8ed62-e6f1-44ed-acc7-9619bd58865a

reference_wrapper Class

Wraps a reference.

Syntax

template <classTy> classreference_wrapper { typedef Ty type; reference_wrapper(Ty&) noexcept; operator Ty&() constnoexcept; Ty& get() constnoexcept; template <class... Types> autooperator()(Types&&... args) const -> decltype(std::invoke(get(), std::forward<Types>(args)...)); };

Remarks

A reference_wrapper<Ty> is a copy constructible and copy assignable wrapper around a reference to an object or a function of type Ty, and holds a pointer that points to an object of that type. A reference_wrapper can be used to store references in standard containers, and to pass objects by reference to std::bind.

The type Ty must be an object type or a function type, or a static assert fails at compile time.

The helper functions std::ref and std::cref can be used to create reference_wrapper objects.

Members

Constructors

NameDescription
reference_wrapperConstructs a reference_wrapper.

Typedefs

NameDescription
result_typeThe weak result type of the wrapped reference.
typeThe type of the wrapped reference.

Functions

NameDescription
getObtains the wrapped reference.

Operators

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

get

Obtains the wrapped reference.

Ty& get() constnoexcept;

Remarks

The member function returns the wrapped reference.

Example

// std__functional__reference_wrapper_get.cpp// compile with: /EHsc #include<functional> #include<iostream>intmain() { 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 

operator Ty&

Gets the wrapped reference.

operator Ty&() constnoexcept;

Remarks

The member operator returns *ptr.

Example

// std__functional__reference_wrapper_operator_cast.cpp// compile with: /EHsc #include<functional> #include<iostream>intmain() { 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 

operator()

Calls the wrapped reference.

template <class... Types> autooperator()(Types&&... args);

Parameters

Types
The argument list types.

args
The argument list.

Remarks

The template member operator() returns std::invoke(get(), std::forward<Types>(args)...).

Example

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

reference_wrapper

Constructs a reference_wrapper.

reference_wrapper(Ty& val) noexcept;

Parameters

Ty
The type to wrap.

val
The value to wrap.

Remarks

The constructor sets the stored value ptr to &val.

Example

// std__functional__reference_wrapper_reference_wrapper.cpp// compile with: /EHsc #include<functional> #include<iostream>intneg(int val) { return (-val); } intmain() { 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 

result_type

The weak result type of the wrapped reference.

typedef R result_type;

Remarks

The result_type typedef is a synonym for the weak result type of a wrapped function. This typedef is only meaningful for function types.

Example

// std__functional__reference_wrapper_result_type.cpp// compile with: /EHsc #include<functional> #include<iostream>intneg(int val) { return (-val); } intmain() { 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 

type

The type of the wrapped reference.

typedef Ty type;

Remarks

The typedef is a synonym for the template argument Ty.

Example

// std__functional__reference_wrapper_type.cpp// compile with: /EHsc #include<functional> #include<iostream>intneg(int val) { return (-val); } intmain() { 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 
close