这部分内容虽然属于标准库的一部分,但是从本质上来看,它却增强了 C++ 语言运行时的能力, 这部分内容也相当重要,所以放到这里来进行介绍。
std::function<>
Lambda 表达式的本质是一个和函数对象类型相似的类类型(称为闭包类型)的对象(称为闭包对象), 当 Lambda 表达式的捕获列表为空时,闭包对象还能够转换为函数指针值进行传递,例如:
#include <iostream> using foo = void(int); // 定义函数类型, using 的使用见上一节中的别名语法 void functional(foo f) { // 定义在参数列表中的函数类型 foo 被视为退化后的函数指针类型 foo* f(1); // 通过函数指针调用函数 } int main() { auto f = [](int value) { std::cout << value << std::endl; }; functional(f); // 传递闭包对象,隐式转换为 foo* 类型的函数指针值 f(1); // lambda 表达式调用 return 0; } |
上面的代码给出了两种不同的调用形式,一种是将 Lambda 作为函数类型传递进行调用, 而另一种则是直接调用 Lambda 表达式,在 C++11 中,统一了这些概念,将能够被调用的对象的类型, 统一称之为可调用类型。而这种类型,便是通过 std::function
引入的。
C++11 std::function
是一种通用、多态的函数封装, 它的实例可以对任何可以调用的目标实体进行存储、复制和调用操作, 它也是对 C++ 中现有的可调用实体的一种类型安全的包裹(相对来说,函数指针的调用不是类型安全的), 换句话说,就是函数的容器。当我们有了函数的容器之后便能够更加方便的将函数、函数指针作为对象进行处理。 例如:
#include <functional> #include <iostream> int foo(int para) { return para; } int main() { // std::function 包装了一个返回值为 int, 参数为 int 的函数 std::function<int(int)> func = foo; std::function<int(int)> func0 = [](int val)->int{return val;}; int important = 10; std::function<int(int)> func2 = [](int value) -> int { return 1+value; }; std::function<float(float)> func3 = [&](float num) -> float{ return 1.3+num+important; }; std::function<double(double)> func4 = [&](double num) -> double{ return 1.22+num + important; }; std::function<void(int)> func5 = [&](int num) -> void{ std::cout<<num+90; }; std::cout << func(10) << std::endl; std::cout << func0(10) << std::endl; std::cout << func2(10) << std::endl; std::cout << func3(1.5) << std::endl; std::cout << func3(15.55) << std::endl; func5(5); std::cout<<std::endl; } std::bind and std::placeholder而
|