Chinaunix首页 | 论坛 | 博客
  • 博客访问: 716102
  • 博文数量: 26
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 3182
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 14:29
个人简介

7年游戏服务器开发,擅长c/c++,javesript,php;熟悉linux,mysql/redis,elasticsearch;开源爱好者.github : https://github.com/yuyunliuhen

文章分类

全部博文(26)

文章存档

2016年(1)

2015年(3)

2014年(3)

2013年(19)

分类: C/C++

2014-05-06 16:06:31


点击(此处)折叠或打开

  1. #include <functional>
  2. #include <numeric>
  3. #include <iostream>

  4. //    help function
  5. int f(int n,char c,double d)
  6. {
  7.     std::cout << n << std::endl;
  8.     std::cout << c << std::endl;
  9.     std::cout << d << std::endl;
  10.     return 1;
  11. }

  12. int g(int) { return 2; }

  13. // g() is overloaded
  14. double g(double){ return 3;}

  15. // take something you can call using ()
  16. struct int_div {            
  17.     float operator()(int x, int y) const { return ((float)x)/y; };
  18. };

  19. struct X {
  20.     int foo(int) { return 4; };
  21. };



  22. void bind_test()
  23. {
  24.     //    std::placeholders,it looks like closure at javascript.
  25.     //    The std::placeholders namespace contains the placeholder objects [_1, . . . _N] where N is an implementation defined maximum number.
  26.     //    When used as an argument in a std::bind expression, the placeholder objects are stored in the generated function object,
  27.     //    and when that function object is invoked with unbound arguments, each placeholder _N is replaced by the corresponding Nth unbound argument.

  28.     // deduce return type
  29.     auto ff = std::bind(f,std::placeholders::_1,'c',1.2);
  30.     // f(7,'c',1.2);
  31.     int x = ff(7);

  32.     auto fff = std::bind(f,std::placeholders::_1,std::placeholders::_2,1.2);
  33.     // f(8,'d',1.2);
  34.     x = fff(8,'d');

  35.     auto ffff = std::bind(f,9,'e',1.2);
  36.     // f(9,'e',1.2);
  37.     x = ffff();

  38.     // reverse argument order
  39.     auto frev = std::bind(f,std::placeholders::_3,std::placeholders::_2,std::placeholders::_1);    
  40.     // f(7,'c',1.2);
  41.     x = frev(1.2,'c',7);    

  42.     // error: which g()?
  43.     //auto g1 = std::bind(g,std::placeholders::_1);        
  44.     // ok (but ugly)
  45.     auto g2 = std::bind((double(*)(double))g,std::placeholders::_1);    

  46.     // explicit return type
  47.     auto f2 = std::bind<int>(f,7,'c',std::placeholders::_1);    
  48.     // f(7,'c',1.2);
  49.     x = f2(1.2);    
  50. }

  51. void function_test()
  52. {
  53.     //    function is a type that can hold a value of just about anything you can invoke using the (...) syntax.
  54.     //    In particular, the result of bind can be assigned to a function. function is very simple to use. For example:
  55.     // make a function object
  56.     std::function<float (int x, int y)> f;    
  57.     // assign
  58.     f = int_div();                    
  59.     // call through the function object
  60.     std::cout << f(5, 3) << std::endl;        
  61.     // passes beautifully
  62.     int init = 100;
  63.     int numbers[] = {10,20,30};
  64.     std::cout << std::accumulate(numbers,numbers + 2,init,f) << std::endl;    

  65.     //    Member functions can be treated as free functions with an extra argument(this pointer)
  66.     std::function<int (X*, int)> f1;
  67.     // pointer to member
  68.     //    something
  69.     //    not support by vs2012?
  70.     //    gcc version 4.8.2 is
  71.     f1 = &X::foo;        
  72.     X x;
  73.     // call X::foo() for x with 5
  74.     int v = f1(&x, 5);    
  75.     // first argument for f is &x
  76.     std::function<int (int)> ff1 = std::bind(f1,&x,std::placeholders::_1);    
  77.     // call x.foo(5)
  78.     v=ff1(5);
  79.     
  80. }
  81. int main()
  82. {
  83.     bind_test();
  84.     function_test();
  85.     return 0;
  86. }

注意:linux下,gcc version4.82如果不能识别auto,编译时加上-std=c++11,比如: g++ -g -o function_bind_test function_bind_test.cc 

reference from:

std::placeholders::_1, std::placeholders::_2, ..., std::placeholders::_N

C++11 - the new ISO C++ standard

std::accumulate


阅读(3007) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~