Suppose I have a std::vector
(for performance reasons I have pointers not actual Obj
s).
I populate it with obj.push
Yes, but you need a functor:
struct delete_ptr
{
template
void operator()(T* pPtr)
{
delete pPtr;
}
};
std::for_each(objs.begin(), objs.end(), delete_ptr());
In C++0x, lambda's help you make functors in-place:
std::for_each(objs.begin(), objs.end(), [](Obj* pPtr){ delete pPtr; });
However, this is dangerous, in the face of exceptions. sbi has shown a solution.