c++比较std::function,以及target()的坑

1.c++ std::function调用member function的方法

 
  1. class Button

  2. {

  3. public:

  4. void onclick(int value)

  5. {

  6. cout<<"will print value\n";

  7. }

  8. };

  9. int main(int argc,char *argv[])

  10. {

  11. function<void(Button,int)> buttonClick;

  12. buttonClick = &Button::onclick;

  13. Button button;

  14. buttonClick(button,1);

  15. return 0;

  16. }

 
  1. #include<iostream>

  2. #include<functional>

  3. using std::cout;

  4. using std::function;

  5. class Button

  6. {

  7. public:

  8. void onclick(int value)

  9. {

  10. cout<<"will print value\n";

  11. }

  12. };

  13. int main(int argc,char *argv[])

  14. {

  15. function<void(int)> buttonClick = std::bind(&Button::onclick, button, std::placeholders::_1);

  16. buttonClick(1);

  17. return 0;

  18. }

2.比较std::function

这里用到了function的target(),根据官方文档来看,这个函数返回指向存储在函数对象中的可调用对象的指针。先来看一个实例。

 
  1. #include <functional>

  2. #include <iostream>

  3. struct foo{ int f(int a) { return a; } };

  4. int fun(int a) { return a; }

  5. int main()

  6. {

  7. // fn1 and fn2 have the same type, but their targets do not

  8. foo f;

  9. std::function<int(int)> fn1(std::bind(&foo::f, &f, std::placeholders::_1));

  10. std::function<int(int)> fn2(fun);

  11. std::cout << fn1.target_type().name() << '\n';

  12. std::cout << fn2.target_type().name() << '\n';

  13. typedef int(*funptr)(int);

  14. funptr *fptr = fn1.target<funptr>();

  15. if(!fptr) {

  16. std::cout << "fn1 nullptr\n";

  17. }

  18. fptr = fn2.target<funptr>();

  19. if(!fptr) {

  20. std::cout << "fn2 nullptr\n";

  21. }

  22. }

在我们运行完后,居然发现fn1是个nullptr,这是为什么呢?原因是这里我们使用了std::bind(),所以我们此时实际的target的参数并不是funptr,而是

std::_Bind<int (foo::*(foo *, std::_Placeholder<1>)(int)>;

所以我们的代码应该改为:

 
  1. #include <functional>

  2. #include <iostream>

  3. struct foo{ int f(int a) { return a; } };

  4. int fun(int a) { return a; }

  5. int main()

  6. {

  7. // fn1 and fn2 have the same type, but their targets do not

  8. foo f;

  9. std::function<int(int)> fn1(std::bind(&foo::f, &f, std::placeholders::_1));

  10. std::function<int(int)> fn2(fun);

  11. std::cout << fn1.target_type().name() << '\n';

  12. std::cout << fn2.target_type().name() << '\n';

  13. auto fptr = fn1.target<std::_Bind<int (foo::*(foo *, std::_Placeholder<1>)(int)>>();

  14. if(!fptr) {

  15. std::cout << "fn1 nullptr\n";

  16. }

  17. }

那么我是怎么确定这个参数的呢,需要用到std::function的另一个方法:target_type().name()。

即上例的fn1.target_type.name(),我们在得到了该值后,在命令行运行如下命令:

c++filt -t St5_BindIFSt7_Mem_fnIM3barFiiiEEPS1_St12_PlaceholderILi1EES6_ILi2EEEE

最后的那个参数是我的实例中打印出的,你可以根据你的情况修改。

部分源码采用一位大佬的,来看看我的源码和结果。

 
  1. #include<iostream>

  2. #include<functional>

  3. using std::cout;

  4. using std::function;

  5. class Button

  6. {

  7. public:

  8. void onclick(int value)

  9. {

  10. cout<<"will print value\n";

  11. }

  12. };

  13. int main(int argc,char *argv[])

  14. {

  15. Button button;

  16. function<void(int)> buttonFun = std::bind(&Button::onclick, button, std::placeholders::_1);

  17. std::cout<<buttonFun.target_type().name()<<"\n";

  18. std::cout<<buttonFun.target<std::_Bind<void (Button::*(Button, std::_Placeholder<1>))(int)>>();

  19. (*buttonFun.target<std::_Bind<void (Button::*(Button, std::_Placeholder<1>))(int)>>())(1);

  20. return 0;

  21. }

 
  1. user@ubuntu:~/study/c++/test$ ./test

  2. St5_BindIFM6ButtonFviES0_St12_PlaceholderILi1EEEE

  3. 0x55ccc430ee70will print value

c++比较std::function,以及target()的坑_std::function target-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值