2011-11-12 23 views
1

如何模板這些函數?在嵌套增強綁定中使用模板函數

boost::function< void(int) > binder(void(*func)(int, char), int a1, char a2) 
{ 
    return boost::bind(func, a1, a2); 
} 

void proxy(boost::function< void(int) > func, int a1) 
{ 
    boost::bind(func, a1)(); 
} 

我嘗試沒有成功如下:

template< typename R, typename A1, typename A2 > 
static boost::function< void(int) > binder(R(*func)(A1,A2), A1 a1, A2 a2) 
{ 
    return boost::bind(func, a1, a2); 
} 

template< typename A1 > 
static void proxy(boost::function< void(A1) > func, A1 a1) 
{ 
    boost::bind(func, a1)(); 
} 

這將是很好,如果我可以不用做粘結劑()。這就是我打算如何使用它們:

void print(int i, char c); 
boost::signals2::signal.connect(
    boost::bind(
     &proxy, 
     boost::bind(
      &binder, 
      &print, 
      _1, 
      'a' 
      ), 
     _1 
     ) 
    ); 

我檢查了,沒有運氣以下:

how-to-use-manipulate-return-value-from-nested-boostbind

perform-argument-substitution-on-nested-boostbind-without-composition

can-i-use-boost-bind-with-a-function-template

回答

2

你需要拼函數指針右:

R(*func)(A1, A2) 

您還需要形成一個函數指針指定模板參數:請記住,binder的功能,但模板

&binder<void, int, char> 
&proxy<int> 

最後,你沒有得到你的信號變量的權利。聲明這樣的:

boost::signals2::signal<void(int)> sig; 

然後使用:

sig.connect(/* all that stuff */); 
+0

我申請在發佈代碼的變化。仍然沒有編譯。 – user1005752

+0

@ user1005752:已更新。 –

+0

謝謝。當我推斷出模板參數時,我仍然有點困惑。我會牢記你上面提到的。對信號2部分抱歉。我意識到這一部分,但是試圖「減少」我發佈的代碼。 – user1005752