STL: calling bind2nd with ptr_fun for "const T &" types
Calling
std::count_if(vec.begin(), vec.end(), std::bind2nd(std::ptr_fun(foo), 17))
works fine with
bool foo(int, int),
but I can't make it work with
bool foo(const开发者_如何学Go int &, const int &)
Is there a way to make that work or do I have to write my own adaptor function?
The second argument is a number, and cannot be converted to const int &
.
You can use boost::bind
to do the trick:
std::count_if (vec.begin(), vec.end(), boost::bind (foo, _1, 17));
EDIT:
As of my first response, yes, you cannot use a variable instead of the number. I think the problem is bind2nd
and ptr_fun
not being properly defined to dereference the type in the case it is a reference when ptr_fun
builds the internal Operation
object, so ither go with boost or write your own functor class.
精彩评论