You cannot, because this violates the most basic rule unique_ptr : there must be only one instance that contains the specified pointer, and unique_ptr has full ownership of it (when it goes beyond the scope, the tocher is deleted).
unique_ptr<T> and unique_ptr<U> (where U : T ) are incompatible, as you saw.
For shared_ptr , for which you can have multiple instances, there is std::static_pointer_cast , which behaves like a static_cast , except that it takes shared_ptr and returns another (and both point to the same object).
If you absolutely must use unique_ptr , you will need to create a function that first disables your current unique_ptr and puts this pointer in the new correct type. You may also need to do the inverse transformation after calling the function.
zneak
source share