Sign in to follow this  
Ro_Akira

C++ explicit destructor call on built-in types

Recommended Posts

I've tried searching for information on this. I don't understand why this compiles okay:
template <typename Type>
void Destroy (Type* object)
{
	object->~Type ();
}

int* j;
Destroy (j);

Ignoring lack of initialization etc., why does that appear to compile okay with a built-in type? If that compiles, then why don't these compile? :
template <typename Type>
Type Double (Type object)
{
	return object.operator+ (object);
}

int j;
Double (j);
j.~int ();

Share this post


Link to post
Share on other sites
Apologist answer: Because of how STL allocation interfaces work, it's useful to be able to do an explicit destructor call without caring whether the type actually has a destructor.

Realistic answer: C++ is somewhat schizophrenic. Deal with it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this