std::reference_wrapper<T>::operator()
template< class... ArgTypes >
typename std::result_of<T&(ArgTypes&&...)>::type
operator() ( ArgTypes&&... args ) const; |
(с C++11) (до C++17) | |
template< class... ArgTypes >
std::invoke_result_t<T&, ArgTypes...>
operator() ( ArgTypes&&... args ) const noexcept(/* see below */);
| (с C++17) (до C++20) | |
template< class... ArgTypes >
constexpr std::invoke_result_t<T&, ArgTypes...>
operator() ( ArgTypes&&... args ) const noexcept(/* see below */);
| (с C++20) |
Вызывает объект Callable, ссылка на который хранится, как будто с помощью INVOKE(get(), std::forward<ArgTypes>(args)...). Эта функция доступна только если хранящаяся ссылка указывает на объект Callable.
T должен быть полным типом.
Параметры
| args | - | аргументы для передачи вызываемой функции |
Возвращаемое значение
Возвращаемое значение вызываемой функции.
Исключения
| Может выбросить реализационно-зависимые исключения. |
(с C++11) (до C++17) |
noexcept спецификация: noexcept(std::is_nothrow_invocable_v<T&, ArgTypes...>) | (с C++17) |
Пример
#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();
}Вывод:
reference to function called bind expression called with 7 as the argument lambda function called
Отчеты об ошибках
Следующие отчеты об ошибках, изменяющих поведение, были применены ретроактивно к ранее опубликованным стандартам C++.
| DR | Применён к | Поведение при публикации | Правильное поведение |
|---|---|---|---|
| LWG 3764 | C++17 | operator() не noexcept | распространять noexcept |
См. также
| обращается к хранящейся ссылке (общедоступный член-функция) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper/operator()