Hello, I need some help using the boost::bind library. I have a map of objects with a string as the key eg map<string, Block*>, what I want to do is call a member function on each Block* in the map using the std::for_each algorithm. The trivial solution would be to craft my own loop and iterate through the list calling the member function on each one. But I'm sure that the boost::bind library can help me out there. class Block { public: void update(int x); }; void main() { map<string, Block*> blocks; for_each(blocks.begin(), blocks.end(), *****); } what should I replace the ***** with to call update() on each block? Any help appreciated! Mark.
From: "Mark Snelling" <mark@bakedbeans.com>
Hello, I need some help using the boost::bind library. I have a map of objects with a string as the key eg map<string, Block*>, what I want to do is call a member function on each Block* in the map using the std::for_each algorithm. The trivial solution would be to craft my own loop and iterate through the list calling the member function on each one. But I'm sure that the boost::bind library can help me out there.
class Block { public: void update(int x); };
void main() { map<string, Block*> blocks; for_each(blocks.begin(), blocks.end(), *****); }
what should I replace the ***** with to call update() on each block?
std::for_each(blocks.begin(), blocks.end(), boost::bind(&Block::update, boost::bind(&std::map<std::string, Block*>::value_type::second, _1), 5 ) ); if you insist. :-) Another option: template<class It, class F> void for_each_pair(It first, It last, F f) { for(; first != last; ++first) { f(first->first, first->second); } } int main() { std::map<std::string, Block*> blocks; for_each_pair(blocks.begin(), blocks.end(), boost::bind(&Block::update, _2, 5)); }
Thanks for the help. I have another question though on a similar topic... Say I have a function: void deleter(B* b) { delete b; } and a list: std::list<Object*> mylist; I can call the for_each algorithm on this list using my function above: std::for_each(mylist.begin(), mylist.end(), &deleter); and it will delete all of the objects pointed to in the list. Is it possible to convert deleter into a templated function: template<class T> void deleter(T* t) { delete t; } and pass that into the for_each algorithm? If so, How? Mark. Peter Dimov wrote:
From: "Mark Snelling" <mark@bakedbeans.com>
Hello, I need some help using the boost::bind library. I have a map of objects with a string as the key eg map<string, Block*>, what I want to do is call a member function on each Block* in the map using the std::for_each algorithm. The trivial solution would be to craft my own loop and iterate through the list calling the member function on each one. But I'm sure that the boost::bind library can help me out there.
class Block { public: void update(int x); };
void main() { map<string, Block*> blocks; for_each(blocks.begin(), blocks.end(), *****); }
what should I replace the ***** with to call update() on each block?
std::for_each(blocks.begin(), blocks.end(), boost::bind(&Block::update, boost::bind(&std::map<std::string, Block*>::value_type::second, _1), 5 ) );
if you insist. :-)
Another option:
template<class It, class F> void for_each_pair(It first, It last, F f) { for(; first != last; ++first) { f(first->first, first->second); } }
int main() { std::map<std::string, Block*> blocks; for_each_pair(blocks.begin(), blocks.end(), boost::bind(&Block::update, _2, 5)); }
Info: <http://www.boost.org> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl> Unsubscribe: <mailto:boost-users-unsubscribe@yahoogroups.com>
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
From: "Mark Snelling" <mark@bakedbeans.com> [...]
std::list<Object*> mylist;
I can call the for_each algorithm on this list using my function above:
std::for_each(mylist.begin(), mylist.end(), &deleter);
and it will delete all of the objects pointed to in the list. Is it possible to convert deleter into a templated function:
template<class T> void deleter(T* t) { delete t; }
and pass that into the for_each algorithm?
Yes. You can use std::for_each(mylist.begin(), mylist.end(), &deleter<Object>); If this doesn't work, try #include <boost/checked_delete.hpp> std::for_each(mylist.begin(), mylist.end(), boost::checked_deleter<Object>()); An alternative is to convert mylist to std::list< boost::shared_ptr<Object > mylist; and let it worry about the lifetime of the objects.
Thanks again Peter. The first solution was actually the one I was trying to compile but was getting linker errors trying to find the body of the concrete function (using MSVC 7) - wierd. Peter Dimov wrote:
From: "Mark Snelling" <mark@bakedbeans.com> [...]
std::list<Object*> mylist;
I can call the for_each algorithm on this list using my function above:
std::for_each(mylist.begin(), mylist.end(), &deleter);
and it will delete all of the objects pointed to in the list. Is it possible to convert deleter into a templated function:
template<class T> void deleter(T* t) { delete t; }
and pass that into the for_each algorithm?
Yes. You can use
std::for_each(mylist.begin(), mylist.end(), &deleter<Object>);
If this doesn't work, try
#include <boost/checked_delete.hpp>
std::for_each(mylist.begin(), mylist.end(), boost::checked_deleter<Object>());
An alternative is to convert mylist to
std::list< boost::shared_ptr<Object > mylist;
and let it worry about the lifetime of the objects.
Info: <http://www.boost.org> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl> Unsubscribe: <mailto:boost-users-unsubscribe@yahoogroups.com>
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Ok, I understand the following: std::for_each(mylist.begin(),mylist.end(),boost::checked_deleter<Object>()); But when I try to use the boost::checked_deleter on the objects held in a map using your solution to my first problem I don't seem to be able to get it to compile: template<class It, class F> void for_each_pair(It first, It last, F f) { for(; first != last; ++first) { f(first->first, first->second); } } int main() { std::map<int, Block*> blocks; std::for_each_pair(blocks.begin(), blocks.end(), boost::bind(boost::checked_delete<Block>(), _2)); } Mark. Peter Dimov wrote:
From: "Mark Snelling" <mark@bakedbeans.com> [...]
std::list<Object*> mylist;
I can call the for_each algorithm on this list using my function above:
std::for_each(mylist.begin(), mylist.end(), &deleter);
and it will delete all of the objects pointed to in the list. Is it possible to convert deleter into a templated function:
template<class T> void deleter(T* t) { delete t; }
and pass that into the for_each algorithm?
Yes. You can use
std::for_each(mylist.begin(), mylist.end(), &deleter<Object>);
If this doesn't work, try
#include <boost/checked_delete.hpp>
std::for_each(mylist.begin(), mylist.end(), boost::checked_deleter<Object>());
An alternative is to convert mylist to
std::list< boost::shared_ptr<Object > mylist;
and let it worry about the lifetime of the objects.
Info: <http://www.boost.org> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl> Unsubscribe: <mailto:boost-users-unsubscribe@yahoogroups.com>
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
From: "Mark Snelling" <mark@bakedbeans.com>
Ok, I understand the following:
std::for_each(mylist.begin(),mylist.end(),boost::checked_deleter<Object>());
But when I try to use the boost::checked_deleter on the objects held in a map using your solution to my first problem I don't seem to be able to get it to compile:
template<class It, class F> void for_each_pair(It first, It last, F f) { for(; first != last; ++first) { f(first->first, first->second); } }
int main() { std::map<int, Block*> blocks; std::for_each_pair(blocks.begin(), blocks.end(), boost::bind(boost::checked_delete<Block>(), _2)); }
checked_delete is a function template (no trailing 'r'); checked_deleter is a class template. :-) Also, on MSVC 6/7 you need to specify a return type when binding general function objects: boost::bind<void>(boost::checked_deleter<Block>(), _2)
Thank you. It may seem that I'm going about this the 'hard' way, but I'm really using it to get a grip on the boost::bind library. Thanks again. Mark. Peter Dimov wrote:
From: "Mark Snelling" <mark@bakedbeans.com>
Ok, I understand the following:
std::for_each(mylist.begin(),mylist.end(),boost::checked_deleter<Object>());
But when I try to use the boost::checked_deleter on the objects held in a map using your solution to my first problem I don't seem to be able to get it to compile:
template<class It, class F> void for_each_pair(It first, It last, F f) { for(; first != last; ++first) { f(first->first, first->second); } }
int main() { std::map<int, Block*> blocks; std::for_each_pair(blocks.begin(), blocks.end(), boost::bind(boost::checked_delete<Block>(), _2)); }
checked_delete is a function template (no trailing 'r'); checked_deleter is a class template. :-)
Also, on MSVC 6/7 you need to specify a return type when binding general function objects:
boost::bind<void>(boost::checked_deleter<Block>(), _2)
Info: <http://www.boost.org> Wiki: <http://www.crystalclearsoftware.com/cgi-bin/boost_wiki/wiki.pl> Unsubscribe: <mailto:boost-users-unsubscribe@yahoogroups.com>
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
participants (2)
-
Mark Snelling
-
Peter Dimov