2011-08-31 76 views
5

所以我想创建像一个函数:如何使用boost :: bind的结果创建一个函数?

void proxy_do_stuff(boost::bind return_here) 
{ 
    return_here(); // call stuff pased into boost::bind 
} 

而且我可以这样称呼它:

proxy_do_stuff(boost::bind(&myclass::myfunction, this, my_function_argument_value, etc_fun_argument)); 

如何做这种事?

回答

3
#include <boost/bind.hpp> 

template<typename T> 
void proxy_do_stuff(T return_here) 
{ 
    return_here(); // call stuff pased into boost::bind 
} 

struct myclass 
{ 
    void myfunction(int, int) 
    { 
    } 
    void foo() 
    { 
     int my_function_argument_value = 3; 
     int etc_fun_argument= 5; 
     proxy_do_stuff(boost::bind(&myclass::myfunction, this, my_function_argument_value, etc_fun_argument)); 
    } 
}; 

int main() 
{ 
    myclass c; 
    c.foo(); 
    return 0; 
} 
+0

以及如何使用这样的功能?我们不应该把它叫做'proxy_do_stuff (...)'吗? – Rella

+0

我添加了如何使用它的一部分。没有必要惹祸。 –

4

boost :: bind的返回类型是boost :: function类型。见下:

void proxy_do_stuff(boost::function<void()> return_here) 
{ 
    return_here(); // call stuff pased into boost::bind 
} 
+3

+1返回类型实际上是boost :: _ bi :: bind_t ,boost :: _ bi :: list3 ,boost :: _ bi :: value ,boost :: _ bi :: value >>但您将它转换为boost :: function

相关问题