Deleting values from std::vector of different types I need to make a vector of different types and be able to delete the values (please ignore anything else but deleting the values for now). Is the next code safe to delete the values?

#include &lt;memory&gt;
#include &lt;vector&gt;
#include &lt;string&gt;

using MyPtr = std::unique_ptr&lt;void, void(*)(void*)&gt;;

template&lt;typename T&gt;
MyPtr MakePtr(const T&amp; val)
{
    return MyPtr(new T(val), [](void* ptr){delete static_cast&lt;T*&gt;(ptr);});
}

int main()
{
    std::vector&lt;MyPtr&gt; ptrs;

    ptrs.push_back(MakePtr(1));
    ptrs.push_back(MakePtr(1.1));
    ptrs.push_back(MakePtr(1.f));
    ptrs.push_back(MakePtr(std::string("1")));

    return 0;
}
Is the next code safe to delete the values?


No. At the very least, you should probably use reinterpret_cast instead of static_cast.Yes.

That said, it seems you are having the x-y problem. Storing data in C++, when you have heterogenous types in a sequence, should be done using a variant type, not void*; in other words, you should create a system that stores your type information per type/variable instance, instead of discarding all type information - like void* does). You are better off maintaining sepatate element lists (sparse arrays or some kind) by type, than you are with a vector&lt;void*&gt; (or vector&lt;unique_ptr&lt;void&gt;&gt; for that matter).

Have a look at boost::any and boost::variant before implementing your solution. You could use one of those, or a separate (similar) implementation.