例如:
template<class T>
class A;
typedef A<int> B;
B* b = new B();
b->~B(); // 是不能调用析构函数的....会编译失败。
所以需要改成模板类/结构或函数来调用析构函数:
template<class T>
inline static void finalize(T& o) {
o.~T();
}
或者参考STL上实现的“默认删除器”模板结构。
template <class _Ty>
struct libtcpip_default_delete { // default deleter for unique_ptr
constexpr libtcpip_default_delete() noexcept = default;
template <class _Ty2, std::enable_if_t<std::is_convertible_v<_Ty2*, _Ty*>, int> = 0>
libtcpip_default_delete(const libtcpip_default_delete<_Ty2>&) noexcept {}
void operator()(_Ty* _Ptr) const noexcept /* strengthened */ { // free a pointer
static_assert(0 < sizeof(_Ty), "can't delete an incomplete type");
_Ptr->~_Ty();
free(_Ptr);
}
};