std::refere...:operator()(3)
π 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
- Applied to:
π See also π
getβ accesses the stored referenceoperator 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
- 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