Spec-Zone.ru › C++

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

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

Возвращает сохранённую ссылку.

Параметры

(нет)

Возвращаемое значение

Сохранённая ссылка.

Пример

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

См. также

operator()
вызывает сохранённую функцию
(открытая функция-член)

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

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API