Deallocate all memory in a container of pointers I want to erase all memory in a container. Currently I'm using this : 

template&lt;typename Cont&gt;
void clear_mem(Cont &amp;container)
{
    while (!container.empty())
    {
        delete container.back();
        container.pop_back();
    }
}



Should the operation of deleting the memory held by the contents be separated by clearing the container ? 
Is it pointless to make this function a template, since it can only  operate on vector and list ?
This assumes that the container holds raw pointer and it owns the pointers. it is a better idea to then let the container hold smart pointers.

These will automatically clear the object they hold when they get destroyed (using delete by default).

typedef std::unique_ptr&lt;Type&gt; Type_ptr;
std::vector&lt;Type_ptr&gt; container;


and use emplace to fill it;

container.emplace_back(std::make_unique&lt;Type&gt;());