How to assign a destructor to a pointer?

Solution: Use a template wrapper

Taking the address of the destructor isn't allowed.

However you can just make a very simple template function and use that instead:

template<class T>
void destruct(const T* x) {
    x->~T();
}

Now instead, just obtain the pointer from:

destruct<Foo>

You can use eg. std::bind (or a lambda) if you need to bind to an actual object:

std::bind(&destruct<Foo>, foo_ptr);

Please note that once it is bound, it cannot be converted to a raw function pointer.


Sorry, you can't. You are not allowed to take the address of a destructor per [class.dtor]/2:

A destructor is used to destroy objects of its class type. The address of a destructor shall not be taken. A destructor can be invoked for a const, volatile or const volatile object. const and volatile semantics ([dcl.type.cv]) are not applied on an object under destruction. They stop being in effect when the destructor for the most derived object starts.


I need a generate pointer which fits for all destructors, is this possible?

No, it is not. Like it would not be with any other member function.

To call a function on an object, you need to know the type of that object.

And, since you cannot take the address of a destructor, you cannot even store/register one in a "database". However, as Paul showed, you can store a functor to do the job. It'll be a bit ugly to register these for each object in use, but that's what happens when you try to reinvent the type system!

I strongly advise moving away from type erasure (how about some nice inheritance instead?), and moving away from calling destructors yourself.

Tags:

C++