Suppose I have a function that looks like
int *a = new int;
*a = 1;
void *b = a;
int *test = reinterpret_cast<int *>(func());
std::cout << test << std::endl;
std::unique_ptr<int> a(new int);
*a = 1;
std::unique_ptr<void> b = a; // does not compile
error: conversion from ‘std::unique_ptr<int>’ to non-scalar type ‘std::unique_ptr<void>’ requested
unique_ptr holds a deleter that is responsible for executing a
delete-expression) on the pointer, when the lifetime of the
But the expression
delete p when
p is a
void*, is invalid.
So the default functionality of
unique_ptr does not support
void*. I am not sure if it is explicitly forbidden or not to have a
unique_ptr<void>. But if it's not forbidden, then trying to define a custom deleter to do this work, would not be smart.
Instead fix the design.
Don't throw away the type information in the first place.