我有一个场景,我想从unique_ptr(T)转到unique_ptr(void),然后可能回到unique_ptr(T)
unique_ptr(void)需要删除器:
unique_ptr<T> uptr_custom;
uptr_custom.reset(new T(...));
// T -> void
unique_ptr<void,void(*)(void*)> uptr_void(
uptr_custom.release(),
[](void * data) { delete static_cast<T*>(data); });
// void -> T
// ?
我的问题是:
是从unique_ptr(T)移动 - &gt; uniqe_ptr(void)是否正确?
如何从unique_ptr(void)返回unique_ptr(T),以使unique_ptr(T)不再具有自定义删除器?
答案 0 :(得分:1)
unique_ptr<T> p;
p.reset(static_cast<T*>(uptr_void.release()));