C++ Functional 库 - operator()
描述
它使用参数 args 调用存储的可调用函数目标。
声明
以下是 std::function::function::operator() 的声明
R operator()( Args... args ) const;
C++11
R operator()( Args... args ) const;
参数
args − 传递给存储的可调用函数目标的参数。
返回值
如果 R 为空,则返回 none。 否则调用存储的可调用对象的返回值。
异常
noexcept:不抛出任何异常。
示例
在下面的 std::function::operator() 示例中。
#include <iostream>
#include <functional>
void call(std::function<int()> f) {
std::cout << f() << '\n';
}
int normal_function() {
return 50;
}
int main() {
int n = 4;
std::function<int()> f = [&n](){ return n; };
call(f);
n = 5;
call(f);
f = normal_function;
call(f);
}
输出应该是这样的 −
4 5 50

