description | title | ms.date | f1_keywords | helpviewer_keywords | ms.assetid | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: reference_wrapper Class |
reference_wrapper Class |
11/04/2016 |
|
|
90b8ed62-e6f1-44ed-acc7-9619bd58865a |
Wraps a reference.
template <class Ty>
class reference_wrapper
{
typedef Ty type;
reference_wrapper(Ty&) noexcept;
operator Ty&() const noexcept;
Ty& get() const noexcept;
template <class... Types>
auto operator()(Types&&... args) const ->
decltype(std::invoke(get(), std::forward<Types>(args)...));
};
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.
Name | Description |
---|---|
reference_wrapper | Constructs a reference_wrapper . |
Name | Description |
---|---|
result_type | The weak result type of the wrapped reference. |
type | The type of the wrapped reference. |
Name | Description |
---|---|
get | Obtains the wrapped reference. |
Name | Description |
---|---|
operator Ty& |
Gets a pointer to the wrapped reference. |
operator() | Calls the wrapped reference. |
Obtains the wrapped reference.
Ty& get() const noexcept;
The member function returns the wrapped reference.
// std__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 the wrapped reference.
operator Ty&() const noexcept;
The member operator returns *ptr
.
// std__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... Types>
auto operator()(Types&&... args);
Types
The argument list types.
args
The argument list.
The template member operator()
returns std::invoke(get(), std::forward<Types>(args)...)
.
// std__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
.
reference_wrapper(Ty& val) noexcept;
Ty
The type to wrap.
val
The value to wrap.
The constructor sets the stored value ptr
to &val
.
// std__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 R result_type;
The result_type
typedef is a synonym for the weak result type of a wrapped function. This typedef is only meaningful for function types.
// std__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;
The typedef is a synonym for the template argument Ty
.
// std__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