Namespaces
Variants

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

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
operator T & ( ) const noexcept ;
(1) (C++11부터)
(C++20부터 constexpr)
T & get ( ) const noexcept ;
(2) (C++11부터)
(C++20부터 constexpr)

저장된 참조를 반환합니다.

목차

매개변수

(없음)

반환값

저장된 참조.

예제

#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->()는 std::reference_wrapper에 대한 참조를 반환하고,
        // reference_wrapper::get()은 map::value_type, 즉 std::pair에 대한 참조를 반환합니다
    assert(m["C"] == 42);
}

참고 항목

저장된 함수를 호출합니다
(public member function)