转载:c++ std::function
【C++】C++11的std::function和std::bind用法详解
1.可调用对象
c++ 中,可调用对象有如下几种定义:
- 函数指针
- 一个具有 operator() 成员函数的类对象
- 一个可被转换为函数指针的类对象
- 一个类成员(函数)指针
实例
- 函数指针
void func(void)
{
//....
}
- 一个具有 operator() 成员函数的类
struct Foo
{
void operator()(void)
{
//...
}
}
- 一个可被转换为函数指针的类对象
struct Bar
{
using fr_t = void(*)(void);
static void func(void)
{
//...
}
operator fr_t(void )
{
return func;
}
};
- 一个类成员(函数)指针
struct A
{
int a_;
void mem_func(void )
{
//...
}
}
调用代码
int main(void)
{
void (* func_ptr)(void) = &func; //1. 函数指针
func_ptr();
Foo foo;
foo(); //2. 一个具有operator()成员函数的类 仿函数
Bar bar;
bar(); //一个可被转换为函数指针的类对象
void (A::*mem_func_ptr)(void) = &A::mem_func; //4 类成员函数指针
int A::*mem_obj_ptr = &A::a_; //类成员指针
A aa;
(aa.*mem_func_ptr)();
aa.*mem_obj_ptr = 123;
retrun 0;
}
从上述看出,除了类的成员指针调用 aa.*mem_obj_ptr = 123;
,其他的调用均可以像一个函数一样做调用操作,即 func_ptr(); foo(); bar(); (aa.*mem_func_ptr)();
所以可调用对象五花八门,当我们试图用统一的方式来保存,或传递一个可调用对象时,会十分繁琐。
由此引入了 std::funcution 和 std::bind, 统一了可调用对象的各种操作。
2. 可调用对象包装器 std::function
std::function 是一个函数包装器,该函数包装器模板能包装任何类型的可调用实体,如普通函数,函数对象,lamda 表达式等。包装器可拷贝,移动等,并且包装器类型仅仅依赖于调用特征,而不依赖于可调用元素自身的类型。std::function 是 C++11 的新特性,包含在头文件中。
一个 std::function 类型对象实例可以包装下列这几种可调用实体:函数、函数指针、成员函数、静态函数、lamda 表达式和函数对象。std::function 对象实例可被拷贝和移动,并且可以使用指定的调用特征来直接调用目标元素。当 std::function 对象实例未包含任何实际可调用实体时,调用该 std::function 对象实例将抛出 std::bad_function_call 异常。
实例
1. 包装普通函数
#include <iostream>
#include <functional>
using namespace std;
int g_Minus(int i, int j)
{
return i - j;
}
int main()
{
function<int(int, int)> f = g_Minus;
cout << f(1, 2) << endl; // -1
return 1;
}
2. 包装模板函数
#include <iostream>
#include <functional>
using namespace std;
template <class T>
T g_Minus(T i, T j)
{
return i - j;
}
int main()
{
function<int(int, int)> f = g_Minus<int>;
cout << f(1, 2) << endl; // -1
return 1;
}
3. 包装 lambda 表达式
#include <iostream>
#include <functional>
using namespace std;
auto g_Minus = [](int i, int j){ return i - j; };
int main()
{
function<int(int, int)> f = g_Minus;
cout << f(1, 2) << endl; // -1
return 1;
}
4. 包装函数对象
非模板类型
#include <iostream>
#include <functional>
using namespace std;
struct Minus
{
int operator() (int i, int j)
{
return i - j;
}
};
int main()
{
function<int(int, int)> f = Minus();
cout << f(1, 2) << endl; // -1
return 1;
}
模板类型:
#include <iostream>
#include <functional>
using namespace std;
template <class T>
struct Minus
{
T operator() (T i, T j)
{
return i - j;
}
};
int main()
{
function<int(int, int)> f = Minus<int>();
cout << f(1, 2) << endl; // -1
return 1;
}
5. 包装类静态成员函数
非模板类型
#include <iostream>
#include <functional>
using namespace std;
class Math
{
public:
static int Minus(int i, int j)
{
return i - j;
}
};
int main()
{
function<int(int, int)> f = &Math::Minus;
cout << f(1, 2) << endl; // -1
return 1;
}
模板类型:
#include <iostream>
#include <functional>
using namespace std;
class Math
{
public:
template <class T>
static T Minus(T i, T j)
{
return i - j;
}
};
int main()
{
function<int(int, int)> f = &Math::Minus<int>;
cout << f(1, 2) << endl; // -1
return 1;
}
6. 包装类对象成员函数
非模板类型:
#include <iostream>
#include <functional>
using namespace std;
class Math
{
public:
int Minus(int i, int j)
{
return i - j;
}
};
int main()
{
Math m;
function<int(int, int)> f = bind(&Math::Minus, &m, placeholders::_1, placeholders::_2);
cout << f(1, 2) << endl; // -1
return 1;
}
模板类型:
#include <iostream>
#include <functional>
using namespace std;
class Math
{
public:
template <class T>
T Minus(T i, T j)
{
return i - j;
}
};
int main()
{
Math m;
function<int(int, int)> f = bind(&Math::Minus<int>, &m, placeholders::_1, placeholders::_2);
cout << f(1, 2) << endl; // -1
return 1;
}
7. 拷贝、移动
int callback(int p)
{
//...
return p;
}
int main(int argc, char *argv[]){
std::cout << "Hello world" << std::endl;
std::function<int(int)> callback2 = callback; //拷贝赋值运算符
std::cout << callback2(7) << std::endl;
std::function<int(int)>&& callback3 = std::move(callback); //移动赋值运算符
std::cout << callback3(7) << std::endl;
std::cout << callback(7) << std::endl;
std::function<int(int)> callback4(callback); //拷贝
std::cout << callback4(7) << std::endl;
return 0;
}
在设计回调函数的时候,无可避免地会接触到可回调对象。在 C++11 中,提供了std::function
和std::bind
两个方法来对可回调对象进行统一和封装。
可调用对象
C++ 中有如下几种可调用对象:函数、函数指针、lambda 表达式、bind 对象、函数对象。其中,lambda 表达式和 bind 对象是 C++11 标准中提出的 (bind 机制并不是新标准中首次提出,而是对旧版本中 bind1st 和 bind2st 的合并)。个人认为五种可调用对象中,函数和函数指针本质相同,而 lambda 表达式、bind 对象及函数对象则异曲同工。
函数
这里的函数指的是普通函数,没什么可拓展的。
函数指针
插播一下函数指针和函数类型的区别:
- 函数指针指向的是函数而非对象。和其他指针类型一样,函数指针指向某种特定类型;
- 函数类型由它的返回值和参数类型决定,与函数名无关。
例如:
bool fun(int a, int b)
上述函数的函数类型是:bool(int, int)
上述函数的函数指针 pf 是:bool (*pf)(int, int)
一般对于函数来说,函数名即为函数指针:
# include <iostream>
int fun(int x, int y) { //被调用的函数
std::cout << x + y << std::endl;
return x + y;
}
int fun1(int (*fp)(int, int), int x, int y) { //形参为函数指针
return fp(x, y);
}
typedef int (*Ftype)(int, int); //定义一个函数指针类型Ftype
int fun2(Ftype fp, int x, int y) {
return fp(x, y);
}
int main(){
fun1(fun, 100, 100); //函数fun1调用函数fun
fun2(fun, 200, 200); //函数fun2调用函数fun
}
编译并运行:
yngzmiao@yngzmiao-virtual-machine:~/test$ g++ main.cc -o main -std=C++11
yngzmiao@yngzmiao-virtual-machine:~/test$ ./main
200
400
可以看出,函数指针作为参数,可以在调用函数中调用函数指针代表的函数内容。
lambda 表达式
lambda 表达式就是一段可调用的代码。主要适合于只用到一两次的简短代码段。由于 lambda 是匿名的,所以保证了其不会被不安全的访问:
# include <iostream>
int fun3(int x, int y){
auto f = [](int x, int y) { return x + y; }; //创建lambda表达式,如果参数列表为空,可以省去()
std::cout << f(x, y) << std::endl; //调用lambda表达式
}
int main(){
fun3(300, 300);
}
关于 lamdba 表达式的内容,可以参考博文:C++ 11 Lambda 表达式。
bind 对象
std::bind 可以用来生产,一个可调用对象来适应原对象的参数列表。具体的内容会在下文讲解。
函数对象
重载了函数调用运算符()
的类的对象,即为函数对象。
std::function
由上文可以看出:由于可调用对象的定义方式比较多,但是函数的调用方式较为类似,因此需要使用一个统一的方式保存可调用对象或者传递可调用对象。于是,std::function
就诞生了。
std::function 是一个可调用对象包装器,是一个类模板,可以容纳除了类成员函数指针之外的所有可调用对象,它可以用统一的方式处理函数、函数对象、函数指针,并允许保存和延迟它们的执行。
定义 function 的一般形式:
# include <functional>
std::function<函数类型>
例如:
# include <iostream>
# include <functional>
typedef std::function<int(int, int)> comfun;
// 普通函数
int add(int a, int b) { return a + b; }
// lambda表达式
auto mod = [](int a, int b){ return a % b; };
// 函数对象类
struct divide{
int operator()(int denominator, int divisor){
return denominator/divisor;
}
};
int main(){
comfun a = add;
comfun b = mod;
comfun c = divide();
std::cout << a(5, 3) << std::endl;
std::cout << b(5, 3) << std::endl;
std::cout << c(5, 3) << std::endl;
}
std::function 可以取代函数指针的作用,因为它可以延迟函数的执行,特别适合作为回调函数
使用。它比普通函数指针更加的灵活和便利。
故而,std::function 的作用可以归结于:
- std::function 对 C++ 中各种可调用实体 (普通函数、Lambda 表达式、函数指针、以及其它函数对象等) 的封装,形成一个新的可调用的 std::function 对象,简化调用;
- std::function 对象是对 C++ 中现有的可调用实体的一种类型安全的包裹 (如:函数指针这类可调用实体,是类型不安全的)。
类型安全的介绍:C++ 类型安全
std::bind
std::bind 可以看作一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来适应原对象的参数列表。
std::bind 将可调用对象与其参数一起进行绑定,绑定后的结果可以使用 std::function 保存。std::bind 主要有以下两个作用:
- 将可调用对象和其参数绑定成一个仿函数;
- 只绑定部分参数,减少可调用对象传入的参数。
调用 bind 的一般形式:
auto newCallable = bind(callable, arg_list);
该形式表达的意思是:当调用newCallable
时,会调用callable
,并传给它arg_list
中的参数。
需要注意的是:arg_list 中的参数可能包含形如_n 的名字。其中 n 是一个整数,这些参数是占位符,表示 newCallable 的参数,它们占据了传递给 newCallable 的参数的位置。数值 n 表示生成的可调用对象中参数的位置:_1 为 newCallable 的第一个参数,_2 为第二个参数,以此类推。
直接文字可能不那么生动,不如看代码:
#include <iostream>
#include <functional>
class A {
public:
void fun_3(int k,int m) {
std::cout << "print: k = "<< k << ", m = " << m << std::endl;
}
};
void fun_1(int x,int y,int z) {
std::cout << "print: x = " << x << ", y = " << y << ", z = " << z << std::endl;
}
void fun_2(int &a,int &b) {
++a;
++b;
std::cout << "print: a = " << a << ", b = " << b << std::endl;
}
int main(int argc, char * argv[]) {
//f1的类型为 function<void(int, int, int)>
auto f1 = std::bind(fun_1, 1, 2, 3); //表示绑定函数 fun 的第一,二,三个参数值为: 1 2 3
f1(); //print: x=1,y=2,z=3
auto f2 = std::bind(fun_1, std::placeholders::_1, std::placeholders::_2, 3);
//表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f2 的第一,二个参数指定
f2(1, 2); //print: x=1,y=2,z=3
auto f3 = std::bind(fun_1, std::placeholders::_2, std::placeholders::_1, 3);
//表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f3 的第二,一个参数指定
//注意: f2 和 f3 的区别。
f3(1, 2); //print: x=2,y=1,z=3
int m = 2;
int n = 3;
auto f4 = std::bind(fun_2, std::placeholders::_1, n); //表示绑定fun_2的第一个参数为n, fun_2的第二个参数由调用f4的第一个参数(_1)指定。
f4(m); //print: a=3,b=4
std::cout << "m = " << m << std::endl; //m=3 说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的,如m
std::cout << "n = " << n << std::endl; //n=3 说明:bind对于预先绑定的函数参数是通过值传递的,如n
A a;
//f5的类型为 function<void(int, int)>
auto f5 = std::bind(&A::fun_3, &a, std::placeholders::_1, std::placeholders::_2); //使用auto关键字
f5(10, 20); //调用a.fun_3(10,20),print: k=10,m=20
std::function<void(int,int)> fc = std::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2);
fc(10, 20); //调用a.fun_3(10,20) print: k=10,m=20
return 0;
}
编译并运行:
yngzmiao@yngzmiao-virtual-machine:~/test$ g++ main.cc -o main -std=C++11
yngzmiao@yngzmiao-virtual-machine:~/test$ ./main
print: x = 1, y = 2, z = 3
print: x = 1, y = 2, z = 3
print: x = 2, y = 1, z = 3
print: a = 3, b = 4
m = 3
n = 3
print: k = 10, m = 20
print: k = 10, m = 20
由此例子可以看出:
- 预绑定的参数是以值传递的形式,不预绑定的参数要用 std::placeholders(占位符) 的形式占位,从_1 开始,依次递增,是以引用传递的形式;
- std::placeholders 表示新的可调用对象的第几个参数,而且与原函数的该占位符所在位置的进行匹配;
- bind 绑定类成员函数时,第一个参数表示对象的成员函数的指针,第二个参数表示对象的地址,这是因为对象的成员函数需要有 this 指针。并且编译器不会将对象的成员函数隐式转换成函数指针,需要通过 & 手动转换;
- std::bind 的返回值是可调用实体,可以直接赋给 std::function。