Reputation: 119
So I am currently in the process of making my own version of a std::vector to use in kernel mode. However I have ran into an issue: I am not sure how to determine how to properly destruct a given element. For example, you could fill an array with integers and you wouldn't need to do anything to destruct it. But for a class you need to initialize it in a specific location with new(address) Class();
and manually call it's destructor like this Class->~Class()
.
Apparently you can check if a given type has a destructor by using functions from type_traits, but this is simply not available to me seeing that it is not shipped with the WDK.
So my question is, how can I determine how to destruct an object of a given type?
Upvotes: 0
Views: 92
Reputation: 34644
You don't have to. You can call obj.~T()
for any object o
of type T
you wish to destroy. If it has a trivial destructor this call will likely result in no operations emitted by the compiler.
Furthermore, you should also placement new trivial types, even if they don't have an explicitly implemented constructor.
MWE:
#include <type_traits>
#include <new>
using T = int;
int main() {
std::aligned_storage_t<sizeof(int), alignof(int)> storage;
T* p = new(&storage) T;
p->~T(); // results in zero instructions emitted
}
See live demo.
Upvotes: 1