Create src/util/drop.hpp

This commit is contained in:
Mm2PL 2024-07-07 13:30:09 +02:00
parent 7bfb5ac0a4
commit 5f9268811f
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9

23
src/util/drop.hpp Normal file
View file

@ -0,0 +1,23 @@
namespace chatterino {
/**
* Calls the destructor of var immediately.
*
* This is useful before noreturn functions to prevent leaking memory from complex types.
*/
template <typename T>
inline void drop(T &var)
{
var.~T();
}
/**
* Helps you avoid accidentally dropping a pointer not the object behind it.
*/
template <typename T>
inline void drop(T * /*var*/)
{
static_assert(false, "Use delete on a pointer instead of drop().");
}
} // namespace chatterino