2013-02-15 53 views
0

從boost :: bind docs(http://www.boost.org/doc/libs/1_53_0/libs/bind/bind.html#with_functions),「綁定的參數被複制並由內部返回的函數對象保存」,但如果有辦法,我可以獲得複製到這些函數對象的參數?如何獲取綁定到boost :: function中的參數?

即:

#include <boost/function.hpp> 
#include <boost/bind.hpp> 
#include <string> 

using namespace std; 

void doSomthing(std::string str) 
{ 
} 

int main() 
{  
    boost::function<void(void)> func_obj = boost::bind(&doSomthing, "some string"); 
    //how can I get the std::string argument("some string") through func_obj? 
} 

在此先感謝。

+2

你的意思是你想讀出來你func_objc變量?不太可能。 – PlasmaHH 2013-02-15 14:50:57

+0

是的,這就是我的意思。 – cyber4ron 2013-02-15 14:57:55

+0

我們已經提高1. ** 53 **現在,而不是1.35。 – 2013-02-15 15:00:36

回答

0

除了調用Boost.Function對象之外,用Boost.Function對象可以做的事情並不多 - 這是設計。 (你可以複製它,摧毀它,比較NULL,但不是更多)。

考慮下面的代碼:

void Foo() {} 
void Bar (int i) { printf ("%d", i); } 

boost::function<void(void)> fFoo (Foo); 
boost::function<void(void)> fBar = boost::bind (Bar, 23); 

這兩個對象被設計成處理相同。它們是相同的類型,並且表現相同。升壓功能沒有機制來區分它們。

對於在來自Boost.Function(和其他地方)所使用的技術有很大的說明,檢查了內文之書的type erasure talk from Boostcon 2010

+0

謝謝!這對我很有幫助。 – cyber4ron 2013-02-27 16:48:46

相關問題