diff --git a/src/util/drop.hpp b/src/util/drop.hpp new file mode 100644 index 000000000..04d7e3351 --- /dev/null +++ b/src/util/drop.hpp @@ -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 +inline void drop(T &var) +{ + var.~T(); +} + +/** + * Helps you avoid accidentally dropping a pointer not the object behind it. + */ +template +inline void drop(T * /*var*/) +{ + static_assert(false, "Use delete on a pointer instead of drop()."); +} + +} // namespace chatterino