boost::ref and boost:::function

Hello, Could you please tell me how to compare two boost::function objects packet with boost::ref. I have container which has boost::function elements and to add elements is not a problem but to remove particular one I don't know how to do that (compare two boost:ref-s). The problem is shown in the code below. Regards, Bartek //code #include <iostream> #include <list> #include <boost/ref.hpp> #include <boost/function.hpp> struct test{ int a; void operator() (int aa) { a += aa; } }; struct function_list { std::list<boost::function<void (int v)> > fun_list; void add(boost::function<void (int v)> fun) { fun_list.push_back(fun); } void remove(boost::function<void (int v)> fun) { std::list<boost::function<void (int v)> >::iterator iter; //HOW TO IMPLEMETN THIS FIND_IF? //std::find_if(fun_list.begin(), fun_list.end(), MISSING_PART); if(iter != fun_list.end()) fun_list.erase(iter); } }; int main(int argc, char **argv) { test a, b; function_list f; f.add(boost::ref(a)); f.add(boost::ref(b)); //DOES NOT WORK f.remove(boost::ref(a)); }

Hi! the std::find_if function takes as it's third parameter an unary predicate function or function object which evaluates the condition. To implement this you can write your comparison function looking like: template<class FunctionType> struct compare_functions { compare_functions(FunctionType f) : func_(f) {} template<class RefObj> bool operator()(RefObj r)const { return func_==r; //boost::reference_wrapper<T> has an operator T& which is called implicitly } private: FunctionType func_; }; modifications in your code: void remove(boost::function<void (int v)> fun) { std::list<boost::function<void (int v)> >::iterator iter; //IMPLEMENTATION std::find_if(fun_list.begin(), fun_list.end(), compare_functions(fun)); if(iter != fun_list.end()) fun_list.erase(iter); } You could also skip the compare_functions implementation and use boost::lambda and placeholder library to write you code like this: std::find_if(fun_list.begin(), fun_list.end(), _1==fun); With Kind Regards, Ovanes Markarian On Mon, March 12, 2007 09:48, misiu wrote:
Hello,
Could you please tell me how to compare two boost::function objects packet with boost::ref. I have container which has boost::function elements and to add elements is not a problem but to remove particular one I don't know how to do that (compare two boost:ref-s). The problem is shown in the code below.
Regards, Bartek
//code
#include <iostream> #include <list> #include <boost/ref.hpp> #include <boost/function.hpp>
struct test{ int a; void operator() (int aa) { a += aa; } };
struct function_list { std::list<boost::function<void (int v)> > fun_list;
void add(boost::function<void (int v)> fun) { fun_list.push_back(fun); }
void remove(boost::function<void (int v)> fun) { std::list<boost::function<void (int v)> >::iterator iter;
//HOW TO IMPLEMETN THIS FIND_IF? //std::find_if(fun_list.begin(), fun_list.end(), MISSING_PART);
if(iter != fun_list.end()) fun_list.erase(iter); } };
int main(int argc, char **argv) {
test a, b; function_list f;
f.add(boost::ref(a)); f.add(boost::ref(b)); //DOES NOT WORK f.remove(boost::ref(a)); }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Ovanes Markarian napisał(a):
[...]
You could also skip the compare_functions implementation and use boost::lambda and placeholder library to write you code like this: std::find_if(fun_list.begin(), fun_list.end(), _1==fun);
When I tried this solution (really nice one) on MSVC 2003 I got this error: d:\boost_ref_test\main.cpp(18): error C2666: 'boost::operator`=='' : 4 overloads have similar conversions and I don't know what to do (or maybe how to show which operator== shell be taken). Best regards, Bartek

Misiu wrote:
Ovanes Markarian napisał(a):
[...]
You could also skip the compare_functions implementation and use boost::lambda and placeholder library to write you code like this: std::find_if(fun_list.begin(), fun_list.end(), _1==fun);
When I tried this solution (really nice one) on MSVC 2003 I got this error:
d:\boost_ref_test\main.cpp(18): error C2666: 'boost::operator`=='' : 4 overloads have similar conversions
You can't compare two function<> objects with ==. What you can do is compare a function<> with the actual contents. That is, void f(); function<void()> f2( f ); f2 == f; // true f2 == f2; // fails at compile time So if you pass the actual object as 'fun' above, instead of converting it to a function<> first, you should be able to use find_if (or just find).

You can't compare two function<> objects with ==. What you can do is compare a function<> with the actual contents. That is,
void f();
function<void()> f2( f );
f2 == f; // true f2 == f2; // fails at compile time
So if you pass the actual object as 'fun' above, instead of converting it to a function<> first, you should be able to use find_if (or just find).
Thanks a lot, it works. Best regards, Bartek
participants (4)
-
misiu
-
Misiu
-
Ovanes Markarian
-
Peter Dimov