[lambda]How can I call function of objects in std::list while using std::for_each?

Please help me! I want to call a virtual function of objects in std::list
while using std::for_each:
struct DiscardableItem {
void Discard() {
std::cout<<"Discard item!"<

But DiscardableItem::Discard does not be called. How can I make it work? thanks! I think lambda can solve this problem easily.
You don't need boost::lambda here, you can easily solve this with boost::bind std::for_each( items.rbegin(), items.rend(), boost::bind(&DiscardableItem::Discard, _1) ); If you *want* to use boost::lambda I'm sure someone else will help :) Regards, Alex

I am sorry that I did not figure out the whole intention. Beside calling
member function, I hope to delete the object immediately.
I wrote the code at first time:
std::for_each( items.rbegin(), items.rend(),
(_1->*&DiscardableItem::Discard, bind(delete_ptr(), _1)) );
after for_each, objects deleted, but Discard function did not been called.
If using Bind can do the same thing, please tell me. thanks!
2009/3/23 Alex MDC
But DiscardableItem::Discard does not be called. How can I make it work?
thanks! I think lambda can solve this problem easily.
You don't need boost::lambda here, you can easily solve this with boost::bind
std::for_each( items.rbegin(), items.rend(), boost::bind(&DiscardableItem::Discard, _1) );
If you *want* to use boost::lambda I'm sure someone else will help :)
Regards, Alex
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Sun, Mar 22, 2009 at 8:52 PM, 钱晓明
I am sorry that I did not figure out the whole intention. Beside calling member function, I hope to delete the object immediately. I wrote the code at first time:
std::for_each( items.rbegin(), items.rend(), (_1->*&DiscardableItem::Discard, bind(delete_ptr(), _1)) );
after for_each, objects deleted, but Discard function did not been called. If using Bind can do the same thing, please tell me. thanks! 2009/3/23 Alex MDC
At this point, though you probably *can* do what you want with bind,
you may want to consider if it will be easier (and clearer in intent)
to simple write a function object that implements your desired
behavior:
struct Discarder : std::unary_function

I think that your advice is a good choice. Using a function object is more
clearly.
However, if some one know how to do this using Lambda or Bind, please tell
me. I want to know it. thanks!
2009/3/23 Christopher Currie
On Sun, Mar 22, 2009 at 8:52 PM, 钱晓明
wrote: I am sorry that I did not figure out the whole intention. Beside calling member function, I hope to delete the object immediately. I wrote the code at first time:
std::for_each( items.rbegin(), items.rend(), (_1->*&DiscardableItem::Discard, bind(delete_ptr(), _1)) );
after for_each, objects deleted, but Discard function did not been called. If using Bind can do the same thing, please tell me. thanks! 2009/3/23 Alex MDC
At this point, though you probably *can* do what you want with bind, you may want to consider if it will be easier (and clearer in intent) to simple write a function object that implements your desired behavior:
struct Discarder : std::unary_function
{ void operator()( DiscardableItem * pd ) const { pd->Discard(); delete pd; } }; std::for_each( items.rbegin(), items.rend(), Discarder() );
HTH, Christopher _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

钱晓明:
However, if some one know how to do this using Lambda or Bind, please tell me. I want to know it. thanks!
You can use one of (_1->*&DiscardableItem::Discard)() bind(&DiscardableItem::Discard, _1)
participants (4)
-
Alex MDC
-
Christopher Currie
-
Peter Dimov
-
钱晓明