W3cubDocs

/C++

std::reference_wrapper<T>::get, std::reference_wrapper<T>::operator T&

(1)
operator T& () const noexcept;
(since C++11)
(until C++20)
constexpr operator T& () const noexcept;
(since C++20)
(2)
T& get() const noexcept;
(since C++11)
(until C++20)
constexpr T& get() const noexcept;
(since C++20)

Returns the stored reference.

Parameters

(none).

Return value

The stored reference.

Example

#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)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper/get