Search code examples
c++destructorreinterpret-cast

Possible destructor after a variable assignment using reinterpret_cast?


I'm hoping that this isn't an utterly ignorant question, but I can't seem to find any information on a few snippets of code I've been coming across.

Instead of describing it, I'll just give an example:

auto x = reinterpret_cast<T*>(something->data * sizeof(T));
myResult = std::move(*x);
x->~T();

Note that this code exists within a template class, hence the T.

I have a general understanding of reinterpret_cast<> and std::move(). However, I don't quite understand what the statement x->~T() means. Being more familiar with C, I thought it was a logical not of the returned value of <datatype>(). Looking at the syntax though, it makes more sense - to me at least - that it's a destructor of some sorts.

If anyone could shed some light on this, it would be appreciated.


Solution

  • It's an explicit destructor, and it's is usually used in conjunction with placement new. Placement new overlays an object (instantiates an object) in memory that had been pre-allocated, and calls the constructor thereafter(see wiki article).