ptr_fun

/* ptr_fun 是将一个普通的函数适配成一个仿函数(functor).
*  (Convert function pointer to function object)
*  下面的例子使用 ptr_fun 将普通函数(两个参数, 如果有多个参数, 要改用boost::bind)适配成bind1st或bind2nd能够使用的functor,
*  否则对bind1st或bind2nd直接绑定普通函数,则编译出错。
*/

#include <functional>     
#include <iostream>     
using namespace std;    
  
int sum(int arg1, int arg2)    
{    
    std::cout<< "arg1 = " << arg1 << std::endl;    
    std::cout<< "arg2 = " << arg2 << std::endl;    
  
    int sum = arg1 + arg2;    
    std::cout << "sum = " << sum << std::endl;    
  
    return sum;    
}  
  
int main(int argc, char *argv[], char *env[])  
{    
    bind1st(ptr_fun(sum), 1)(2);        // the same as sum(1,2)     
    bind2nd(ptr_fun(sum), 1)(2);        // the same as sum(2,1)     
  
    return 0;  
}  

/*
Output:

arg1 = 1
arg2 = 2
sum = 3
arg1 = 2
arg2 = 1
sum = 3
*/


 

你可能感兴趣的:(ptr_fun)