std::refere...:operatorT&(3)
π NAME π
okay bestie, this is the C++ Standard Libary page for std::reference_wrapper::get,std::reference_wrapper::operatorT& β yes, thatβs the whole double-name situation, and the header up top is literally std::refere...:operatorT&(3) (section (3), no cap π
). The name line is:
std::reference_wrapper::get,std::reference_wrapper::operatorT&βstd::reference_wrapper::get,std::reference_wrapper::operatorT&
β¨ Synopsis β¨
two overloads, both returning the stored reference, both noexcept, both const, and both got that constexpr glow-up in C++20 πΈ:
operator T& () const noexcept; (1) (since C++11)
(constexpr since C++20)
T& get() const noexcept; (2) (since C++11)
(constexpr since C++20)
Returns the stored reference β thatβs the actual line right under the synopsis, bestie β¨
π Parameters π
- Parameters:
(none)β literally no arguments. You donβt pass anything, you just vibe with the stored reference π
π Return value π
Returns the stored reference. Thatβs the whole answer β no plot twist, no hidden side quest β¨
π¦ Example π¦
okay so this is the code, fr fr β including the // Run this code line because we keep every detail around here π«Ά:
// Run this code
#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 β
operator()β calls the stored function (public member function) π₯
kisses bestie π this page is from http://cppreference.com, dated 2024.06.10, and the footer says std::refere...:operatorT&(3) β so thatβs the whole manual-page moment. bye π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms