Search code examples
c++templatesconstructordestructortemplate-function

How to call destructor of type in template?


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?


Solution

  • 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();