|
Boost Users : |
From: Mark Snelling (mark_at_[hidden])
Date: 2002-09-12 15:27:02
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_at_[hidden]>
> [...]
>
>>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_at_[hidden]>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net