C++ 11新特性之std::function类模板与std::bind绑定器介绍

std::function介绍

  • 在c++中,可调用实体主要包括:函数、函数指针、函数引用、可以隐式转换为函数指定的对象,或者实现了operator()的对象
  • c++ 11中,新增加了一个std::function类模板,它是对C++中现有的可调用实体的一种类型安全的包裹。通过指定它的模板参数,可以用统一的方式处理函数、函数对象、函数指针,并允许保存和延迟执行它们。
  • std::function对象最大的用处就是实现函数回调
  • 语法:std::function<返回值类型(参数类型)>
  • 示例
    •   #include <iostream>
        #include <functional>
      
        void func1(){
        	std::cout<<"this is func1"<<std::endl;
        }
      
        int func2(int data1, int data2){
        	std::cout<< data1 <<" + "<< data2 <<" = "<< data1 + data2 << std::endl;
         	return data1 + data2;
        }
      
        class A{
        public:
        static int funcA(){
            	std::cout<<"this is funcA"<<std::endl;
            	return 0;
        	}
        };
      
        int main(){
        	//绑定普通函数()
        	std::function<void()> f1 = func1;
        	//调用
        	f1();
      
        	std::function<int(int, int)> f2 = func2;
        	//调用
        	f2(10, 20);
      
        	//绑定类静态成员函数
        	std::function<int()> fa = A::funcA;
        	//调用
        	fa();
      
      
        	system("pause");
        	return 0;
        }
      
  • 执行结果
    •   this is func1
        10 + 20 = 30
        this is funcA
        请按任意键继续. . .
      

std::bind介绍

  • std::bind是这样一种机制,它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调用实体,这种机制在回调函数的使用过程中也颇为有用。
  • std::bind绑定的参数个数不受限制,绑定的具体哪些参数也不受限制。
  • 语法:auto newCallable = bind(callable, arg_list);
    • newCallable : 可调用对象
    • callable : 要调用的对象
    • arg_list : 逗号分隔的参数列表
  • 示例
    •   #include <iostream>
        #include <functional>
      
        void func1(){
        	std::cout<<"this is func1"<<std::endl;
        }
      
        int func2(int data1, int data2){
        	std::cout<< data1 <<" + "<< data2 <<" = "<< data1 + data2 << std::endl;
        	return data1 + data2;
        }
      
        int main(){
      
        	std::bind(func1)();
        	std::bind(func2, 10, 20)();
      
        	// 函数调用时, std::placeholders::_1 被第一个参数替换, 依此类推
        	std::bind(func2, std::placeholders::_1, std::placeholders::_2)(11, 90);
      
      
        	system("pause");
        	return 0;
        }
      
  • 执行结果
    •   this is func1
        10 + 20 = 30
        11 + 90 = 101
        请按任意键继续. . .
      

std::function与std::bind配合使用

  • 示例
    •   #include <iostream>
        #include <functional>
      
      
        class A{
        public:
        	void func(int x, int y){
        		std::cout << "x = "<< x <<", y = "<< y << std::endl;
        	}
        	int a;
        };
      
        int main(){
      
        	A objA;
      
        	//绑定成员函数
        	std::function<void(int, int)> fa = std::bind(&A::func, &objA, std::placeholders::_1, std::placeholders::_2);
        	//调用
        	fa(11, 22);
      
        	//绑定成员变量
        	std::function<int &()> f2 = std::bind(&A::a, &objA);
        	f2() = 1234;
        	std::cout<<"objA a = "<< objA.a << std::endl;
      
        	system("pause");
        	return 0;
        }
      
  • 运行结果
    •   x = 11, y = 22
        objA a = 1234
        请按任意键继续. . .
      

猜你喜欢

转载自blog.csdn.net/new9232/article/details/128158796