From 5f9268811f0201e9c04d87a6b55f4f5a83037045 Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Sun, 7 Jul 2024 13:30:09 +0200 Subject: [PATCH] Create src/util/drop.hpp --- src/util/drop.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/util/drop.hpp 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