目录

operator()

描述 (Description)

它使用参数args调用存储的可调用函数目标。

声明 (Declaration)

以下是std :: function :: function :: operator()的声明

R operator()( Args... args ) const;

C++11

R operator()( Args... args ) const;

参数 (Parameters)

args - 要传递给存储的可调用函数目标的参数。

返回值 (Return Value)

如果R为空,则返回none。 否则调用存储的可调用对象的返回值。

异常 (Exceptions)

noexcept:它不会抛出任何异常。

例子 (Example)

在下面的例子中,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
↑回到顶部↑
WIKI教程 @2018