2011-08-31 53 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

相關問題