std::refere...:operator()(3)

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

πŸ’– NAME πŸ’–

std::reference_wrapper::operator() - std::reference_wrapper::operator()

okay bestie, this is the page for std::reference_wrapper::operator() from the C++ Standard Libary manual β€” the page header calls it std::refere...:operator()(3), and the manual name is C++ Standard Libary, which I am preserving exactly because we do not edit the source material in this group chat πŸ’…


✨ Synopsis ✨

the synopsis is giving two C++ eras, so here is the exact signature text, character for character:

template< class... ArgTypes >
                                                                              (since
C++11)
          typename std::result_of<T&(ArgTypes&&...)>::type
       (until C++17)

              operator() ( ArgTypes&&... args ) const;
          template< class... ArgTypes >
                                                                              (since
C++17)
          std::invoke_result_t<T&, ArgTypes...>
       (constexpr since
                                                                              C++20)
              operator() ( ArgTypes&&... args ) const noexcept(/* see below
          */);

so what does it do? It calls the Callable object that the stored reference points at, as if by INVOKE(get(), std::forward<ArgTypes>(args)...). This function is available only if the stored reference points to a Callable object.

Also, T must be a complete type. no cap, that part matters 🫢


🌸 Parameters 🌸

  • args β€” arguments to pass to the called function

πŸ¦‹ Return value πŸ¦‹

The return value of the called function. that's it, bestie β€” it literally just gives you back whatever the called function gives back ✨


πŸ”₯ Exceptions πŸ”₯

  • Since C++11 until C++17: may throw implementation-defined exceptions 😭
  • Since C++17 β€” noexcept specification: noexcept(std::is_nothrow_invocable_v<T&, ArgTypes...>)

πŸŽ€ Example πŸŽ€

this example is so cute β€” it shows a reference to a function, a bind expression, and a lambda all getting called through the wrapper. run it and see:

       // Run this code

        #include <functional>
        #include <iostream>

        void f1()
        {
            std::cout << "reference to function called\n";
        }

        void f2(int n)
        {
            std::cout << "bind expression called with " << n << " as the
       argument\n";
        }

        int main()
        {
            std::reference_wrapper<void()> ref1 = std::ref(f1);
            ref1();

            auto b = std::bind(f2, std::placeholders::_1);
            auto ref2 = std::ref(b);
            ref2(7);

            auto c = []{ std::cout << "lambda function called\n"; };
            auto ref3 = std::ref(c);
            ref3();
        }

🌈 Output 🌈

reference to function called
bind expression called with 7 as the argument
lambda function called

πŸ‘‘ Defect reports πŸ‘‘

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR: LWG 3764
    • Applied to: C++17
    • Behavior as published: operator() is not noexcept
    • Correct behavior: propagate noexcept

πŸ’œ See also πŸ’œ

  • get β€” accesses the stored reference
  • operator T& β€” public member function

πŸ“ Category πŸ“

  • conditionally noexcept

source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::refere...:operator()(3) β€” mwah, bestie πŸ’‹πŸ’…βœ¨

πŸ“¦ where this page comes from