2012-04-18 26 views
3

我的問題很簡單:我可以做這樣的事嗎?我可以在mem_fun_ref()中使用boost :: bind()嗎?

說Foo類包含以下成員函數:

foo foo::DoSomething(input_type1 input1, input_type2 input2) 
{ 
    ... // Adjust private datamembers 
    return *this; 
} 

使用富:

std::vector<foo> foovec; 
input_type1 in1; 
input_type2 in2; 
... 
std::transform(foovec.begin(), foovec.end(), foovec.begin(), std::mem_fun_ref(boost::bind(&foo::DoSomething, in1, in2))); 

所以這可能嗎?問題幾乎在於boost::bind()是否對其工作的函數的成員/非成員性質有影響。我想我不能去了解它周圍的其他方法是這樣的:

std::transform(foovec.begin(), foovec.end(), foovec.begin(), boost::bind(std::mem_fun_ref(&foo::DoSomething), _1, in1, in2))); 

因爲std::mem_fun_ref()花費一元或無參函數和DoSomething()二進制。

+1

取代boost::bind如果你打算使用升壓綁定,你不希望使用[Boost.Function](http://www.boost.org/doc/libs/1_49_0/doc/html/function.html)? – 2012-04-18 00:35:45

+2

您使用的是什麼版本的Boost? 'boost :: bind(&foo :: DoSomething,_1,in1,in2)'應該可以開箱即用。 [如文件記錄](http://www.boost.org/doc/libs/1_49_0/libs/bind/bind.html#with_member_pointers)。 'boost :: phoenix :: bind'也一樣(因爲Boost.Phoenix應該是Boost.Lambda的高級替代品,它應該是Boost.Bind的高級替代品)等等。 – 2012-04-18 00:49:13

回答

3

你不需要std::mem_fun_ref,只需使用:

std::transform(foovec.begin(), 
       foovec.end(), 
       foovec.begin(), 
       boost::bind(&foo::DoSomething, _1, in1, in2)); 

,或者你可以用

std::bind(&foo::DoSomething, std::placeholders::_1, in1, in2) 
+0

酷,我想我仍然只是瞭解增強庫的力量。謝謝! – Wouter 2012-04-23 13:47:30

相關問題