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 <memory>
#include <vector>
#include <string>
using MyPtr = std::unique_ptr<void, void(*)(void*)>;
template<typename T>
MyPtr MakePtr(const T& val)
{
return MyPtr(new T(val), [](void* ptr){delete static_cast<T*>(ptr);});
}
int main()
{
std::vector<MyPtr> 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;
}