当前位置:   article > 正文

C++_包装器

C++_包装器

       

目录

1、包装器的用法

2、包装器的类型 

3、包装器的作用 

4、包装成员函数

5、bind(绑定)

5.1 bind的用法         

5.2 bind减少参数个数 

结语


前言:

        C++11的包装器,总称为function包装器,而包装器又称适配器,顾名思义包装器主要是用于包装函数的,实际上是将函数指针、仿函数类、lambda函数进行了又一层的封装。 

1、包装器的用法

       包装器的具体结构如下:

  1. 使用包装器需包头文件:<functional>
  2. // 类模板原型如下
  3. template <class Ret, class... Args>
  4. class function<Ret(Args...)>;
  5. 模板参数说明:
  6. Ret: 被调用函数的返回类型
  7. Args…:可变参数包,表示被调用函数的形参可以有多个

        包装器测试代码如下:

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include<iostream>
  3. #include<functional>
  4. using namespace std;
  5. int f(int a, int b)
  6. {
  7. cout << "int f(int a, int b)" << endl;
  8. return a + b;
  9. }
  10. int main()
  11. {
  12. function<int(int, int)> f1 = f;//包装函数f
  13. cout << f1(1, 2) << endl;//使用包装器调用函数f
  14. return 0;
  15. }

        运行结果:

        从结果可以看到,若要使用包装器封装函数,则包装器的返回类型和参数必须和该函数一致,示意图如下:

        并且包装器仅仅只是封装了函数f,并且生成一个包装器对象,用包装器对象去调用包装的函数时,还是会去调用函数f的本体。 

2、包装器的类型 

        函数指针,lambda函数、仿函数类这三种函数本身是不能够直接进行赋值操作的,因为他们三个各有各的类型,然而经过包装器包装后,产生的三个包装器对象是可以直接进行相互赋值操作的。

        示例代码如下:

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include<iostream>
  3. #include<functional>
  4. using namespace std;
  5. int f(int a, int b)
  6. {
  7. cout << "int f(int a, int b)" << endl;
  8. return a + b;
  9. }
  10. struct Functor
  11. {
  12. public:
  13. int operator() (int a, int b)
  14. {
  15. cout << "int operator() (int a, int b)" << endl;
  16. return a + b;
  17. }
  18. };
  19. int main()
  20. {
  21. function<int(int, int)> f1 = f;
  22. function<int(int, int)> f2 = Functor();
  23. f1 = f2;//f2赋值给f1,则f1调用的是f2的函数,即Functor
  24. cout << f1(1, 2) << endl<<endl;
  25. cout << f2(1, 2) << endl;
  26. return 0;
  27. }

        运行结果:

3、包装器的作用 

         在一些特殊的场景下,比如一个容器要接收函数指针,lambda函数、仿函数类,则该容器的类型晦涩难写,比如函数指针的类型:void(*ptr)(),写起来比较麻烦,更不用说lambda的类型了,就算取到了他们的类型,写出来的代码也属于高耦合的代码,即该容器只能接收三者之一的数据类型。

        而包装器可以实现以上的效果,并且写出来的代码属于低耦合的代码,即以上三种类型的数据都可以写进容器里,具体示例如下:

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include<iostream>
  3. #include<functional>
  4. #include<map>
  5. using namespace std;
  6. int f(int a, int b)
  7. {
  8. cout << "int f(int a, int b)" << endl;
  9. return a + b;
  10. }
  11. struct Functor
  12. {
  13. public:
  14. int operator() (int a, int b)
  15. {
  16. cout << "int operator() (int a, int b)" << endl;
  17. return a + b;
  18. }
  19. };
  20. int main()
  21. {
  22. map < string, function<int(int, int)>> m1;//参数1为string,参数2为包装器
  23. //map的[] 进行插入元素,并返回第二个参数的引用
  24. m1["函数指针"] = f;
  25. m1["仿函数类"] = Functor();
  26. m1["lambda"] = [](int x, int y)->int
  27. {
  28. cout << "[](int x, int y)->int" << endl;
  29. return x + y;
  30. };
  31. cout << m1["函数指针"](1, 2) << endl << endl;
  32. cout << m1["仿函数类"](1, 2) << endl << endl;
  33. cout << m1["lambda"](1, 2) << endl;
  34. return 0;
  35. }

        运行结果:

4、包装成员函数

        用包装器包装成员函数时需要考虑该成员函数是否有this指针,因为普通成员函数含this指针,而静态成员函数不含this指针,若函数中含this指针,则包装器的第一个参数一定是this指针。

        示例代码如下:

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include<iostream>
  3. #include<functional>
  4. using namespace std;
  5. class Wrapper
  6. {
  7. public:
  8. static int Wrapperi(int a, int b)//静态成员函数无this指针
  9. {
  10. return a + b;
  11. }
  12. int Wrapperd(int a, int b)
  13. {
  14. return a + b + res;
  15. }
  16. private:
  17. int res = 2;
  18. };
  19. int main()
  20. {
  21. function<int(int, int)> f1 = Wrapper::Wrapperi;
  22. //Wrapperd含this指针,因此要包装器的参数要多写一个this的类型
  23. function<int(Wrapper, int, int)> f2 = &Wrapper::Wrapperd;//此处要加取地址符
  24. cout << f1(2, 3) << endl;
  25. cout << f2(Wrapper(), 2, 3) << endl;
  26. return 0;
  27. }

         运行结果:

        包装带this指针的另一种写法:

5、bind(绑定)

        bind的格式:

  1. auto newTarget = bind(target,arg_list);
  2. //target是一个函数对象,也可以是指向函数的指针
  3. //arg_list是调整之后的参数列表
  4. //newTarget也是一个函数对象

        bind也是一种函数适配器,他接收一个函数对象,并且对该函数的参数列表进行调整,如形参的顺序、形参的个数,最后返回一个函数对象,该对象可以调用调整之后的函数(即调用newTarget时,实际上是以arg_list为形参列表去调用target),可用包装器接收该newTarget。

5.1 bind的用法         

        测试bind的代码如下:

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include<iostream>
  3. #include<functional>
  4. using namespace std;
  5. void Wrapperi(int a, int b)
  6. {
  7. cout << a << " ";
  8. cout << b << endl;
  9. }
  10. int main()
  11. {
  12. Wrapperi(2, 3);
  13. //将形参顺序倒置
  14. //placeholders表示占位符,placeholders::_2表示Wrapperi的第二个参数现在成了f1的第一个参数
  15. function<void(int, int)> f1 = bind(Wrapperi, placeholders::_2, placeholders::_1);
  16. //直接传参的写法
  17. auto f2 = bind(Wrapperi,20,10);//直接把20和10当作f2的形参
  18. f1(2, 3);//同样的调用顺序,结果实参2给到的是Wrapperi的形参b
  19. f2();
  20. return 0;
  21. }

         运行结果:

         占位符用法的具体示意图如下:

5.2 bind减少参数个数 

         bind可以减少函数对象的参数个数,比如某函数有3个参数,因此调用该函数时需要传3个实参,但是bind可以将该函数减少为2个参数后给到一个新的函数对象,并且新函数对象调用该函数时不需要传3个实参。

        示例代码如下:

  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include<iostream>
  3. #include<functional>
  4. using namespace std;
  5. class Sub
  6. {
  7. public:
  8. int sub(int a, int b)
  9. {
  10. return a - b;
  11. }
  12. };
  13. int main()
  14. {
  15. //没有调整参数的调用方式
  16. std::function<int(Sub,int, int)> func2 = &Sub::sub;
  17. cout << func2(Sub(), 6, 3) << endl;
  18. //使用bind进行参数调整后的调用方式,调用包装器对象从3个参数变成了2个
  19. Sub s;
  20. std::function<int(int, int)> func3 =
  21. std::bind(&Sub::sub,s,placeholders::_1, placeholders::_2);
  22. cout << func3(10, 8) << endl;
  23. }

        运行结果:

        从结果可以发现,bind之所以可以减少参数是因为在bind中已经将该参数显示调用了,所以新函数对象才可以不用再次显示调用该参数。注意:并且func3(包装器)类型的形参个数要和bind减少参数之后的形参个数相匹配

结语

        以上就是关于包装器和bind的讲解,包装器的作用就是为了能够让程序员更好的显示表示函数对象的类型,而bind则是能够让一些函数对象能够相互的兼容,比如A函数对象比B函数对象的形参多一个,其余的参数都一样,为了能够让A和B的类型相等,可以使用bind让A的形参减少一个,这样他们就可以兼容了,比如下面场景:

        最后希望本文可以给你带来更多的收获,如果本文对你起到了帮助,希望可以动动小指头帮忙点赞

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/252541
推荐阅读
相关标签