std::reference_wrapper<T>:: get, std::reference_wrapper<T>:: operator T&
From cppreference.com
<
cpp
|
utility
|
functional
|
reference wrapper
C++
Utilities library
|
|
Function objects
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Old binders and adaptors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
std::reference_wrapper
Member functions | ||||
reference_wrapper::get
reference_wrapper::operator T&
|
||||
Non-member functions | ||||
(C++26)
(C++26)
|
||||
Deduction guides (C++17) | ||||
Helper classes | ||||
operator T
&
(
)
const
noexcept
;
|
(1) |
(since C++11)
(constexpr since C++20) |
T
&
get
(
)
const
noexcept
;
|
(2) |
(since C++11)
(constexpr since C++20) |
Returns the stored reference.
Parameters
(none)
Return value
The stored reference.
Example
Run this code
#include <cassert> #include <functional> #include <map> #include <optional> #include <string_view> using Map = std::map<std::string_view, int>; using Opt = std::optional<std::reference_wrapper<Map::value_type>>; Opt find(Map& m, std::string_view s) { auto it = m.find(s); return it == m.end() ? Opt{} : Opt{*it}; } int main() { Map m{{"A", 1}, {"B", 2}, {"C", 3}}; if (auto opt = find(m, "C"); opt) opt->get().second = 42; // std::optional::operator->() returns reference to std::reference_wrapper, then // reference_wrapper::get() returns reference to map::value_type, i.e. std::pair assert(m["C"] == 42); }
See also
calls the stored function
(public member function) |
Retrieved from "
https://en.cppreference.com/mwiki/index.php?title=cpp/utility/functional/reference_wrapper/get&oldid=171004
"