std::refere...:operatorT&(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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