How to call destructor of type in template?

4Bytes picture 4Bytes · Nov 3, 2012 · Viewed 7.4k times · Source

For example, we have a function like that:

template <typename TYPE>
void construct_and_destruct(TYPE & object)
{
    //...
}

We cant call constructor and destructor like object.Type() and object.~Type() (no true now) ( Whyy? =C )

To call the constructor we can like new(&object) TYPE(). And I dont know how to call destructor (no exist placement delete). How to do this?

Answer

Luchian Grigore picture Luchian Grigore · Nov 3, 2012

You can call the destructor as:

object.~TYPE();

but it's likely not what you want, and are subject to a double delete.

The constructor is as simple as:

object = TYPE();