std::reference_wrapper<T>::operator()

来自cppreference.com
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
旧式绑定器与适配器
(C++17 前*)
(C++17 前*)
(C++17 前*)
(C++17 前*)
(C++17 前*)(C++17 前*)(C++17 前*)(C++17 前*)
(C++20 前*)
(C++20 前*)
(C++17 前*)(C++17 前*)
(C++17 前*)(C++17 前*)

(C++17 前*)
(C++17 前*)(C++17 前*)(C++17 前*)(C++17 前*)
(C++20 前*)
(C++20 前*)
 
 
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(/* 见下文 */);
(C++17 起)
(C++20 起为 constexpr)

如同以 INVOKE(get(), std::forward<ArgTypes>(args)...) 调用存储自身引用的可调用 (Callable) 对象。此函数只有在存储的引用指向可调用 (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 << "调用了到函数的引用\n";
}
 
void f2(int n)
{
    std::cout << "以 " << n << " 作为实参调用了绑定表达式\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 函数\n"; };
    auto ref3 = std::ref(c);
    ref3();
}

输出:

调用了到函数的引用
以 7 作为实参调用了绑定表达式
调用了 lambda 函数

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告应用于出版时的行为正确行为
LWG 3764C++17operator() 不是 noexcept传播 noexcept

参阅

访问存储的引用
(公开成员函数)