Namespaces
Variants

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

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until 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)

调用所存储引用的 Callable 对象,调用方式如同通过 INVOKE ( get() , std:: forward < ArgTypes > ( args ) ... ) 。此函数仅在存储的引用指向 Callable 对象时可用。

T 必须是一个完整类型。

目录

参数

args - 传递给被调用函数的参数

返回值

被调用函数的返回值。

异常

可能抛出实现定义的异常。

(since C++11)
(until C++17)
noexcept specification:
noexcept ( std:: is_nothrow_invocable_v < T & , ArgTypes... > )
(since 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

参见

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