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) (constexpr с 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/cpp/utility/functional/reference_wrapper/operator()