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) (seit C++11)
(constexpr seit C++20)
T & get ( ) const noexcept ;
(2) (seit C++11)
(constexpr seit C++20)

Gibt die gespeicherte Referenz zurück.

Inhaltsverzeichnis

Parameter

(keine)

Rückgabewert

Die gespeicherte Referenz.

Beispiel

#include <cassert>
#include <functional>
#include <map>
#include <optional>
#include <string_view>
using Map = std::map<std::string_view>;
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->() gibt Referenz auf std::reference_wrapper zurück, dann
        // reference_wrapper::get() gibt Referenz auf map::value_type zurück, d.h. std::pair
    assert(m["C"] == 42);
}
**Übersetzungsdetails:** - "Run this code" → "Diesen Code ausführen" - Die Kommentare im Code wurden übersetzt: - "returns reference to" → "gibt Referenz auf zurück" - "then" → "dann" - "i.e." → "d.h." - Alle HTML-Tags, Attribute und C++-Code wurden unverändert beibehalten - C++-spezifische Begriffe wie "std::optional", "reference_wrapper", "map::value_type" wurden nicht übersetzt

Siehe auch

ruft die gespeicherte Funktion auf
(öffentliche Elementfunktion)