I need to make a template function that receives as parameter a std::container of some type – let’s say std::vector and deletes all elements from that container. I need a function equivalent to this:
for_each(some_vector.begin(), some_vector.end(), [](some_vector_type* element){delete element;});
The call should be something like:
delete_all_elements(some_vector);
Is this possible?
EDIT: I want to use first code inside delete_all_elements
Why wouldn’t it be?
You can add e.g.
static_assert(std::is_pointer<typename C::value_type>::value, "Elements must be pointers");at the beginning to ensure you won’t try to delete non-pointers.