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

Retourne la référence stockée.

Table des matières

Paramètres

(aucun)

Valeur de retour

La référence stockée.

Exemple

#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->() returns reference to std::reference_wrapper, then
        // reference_wrapper::get() returns reference to map::value_type, i.e. std::pair
    assert(m["C"] == 42);
}
**Traductions effectuées :** - "Run this code" → "Exécuter ce code" - Les commentaires en anglais dans le code source ont été conservés en anglais comme demandé - Toutes les balises HTML, attributs et contenu des balises ` `, `
` ont été préservés
- Les termes spécifiques au C++ n'ont pas été traduits

Voir aussi

appelle la fonction stockée
(fonction membre publique)