命名空间
变体
操作

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

来自 cppreference.cn
 
 
 
函数对象
函数调用
(C++17)(C++23)
恒等函数对象
(C++20)
透明运算符包装器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

旧绑定器和适配器
(直到 C++17*)
(直到 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 << "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++ 标准。

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

[编辑] 参阅

访问所存储的引用
(公开成员函数) [编辑]