From 76aced691552e193e0225af40f8acf484cfeaefe Mon Sep 17 00:00:00 2001 From: Jade Lovelace Date: Mon, 26 Feb 2024 01:21:54 -0800 Subject: [PATCH] finally.hh: delete copy constructor which is a bad idea --- src/libutil/finally.hh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libutil/finally.hh b/src/libutil/finally.hh index 4cae20a36..f9f0195a1 100644 --- a/src/libutil/finally.hh +++ b/src/libutil/finally.hh @@ -11,8 +11,15 @@ class [[nodiscard("Finally values must be used")]] Finally { private: Fn fun; + bool movedFrom = false; public: Finally(Fn fun) : fun(std::move(fun)) { } - ~Finally() { fun(); } + // Copying Finallys is definitely not a good idea and will cause them to be + // called twice. + Finally(Finally &other) = delete; + Finally(Finally &&other) : fun(std::move(other.fun)) { + other.movedFrom = true; + } + ~Finally() { if (!movedFrom) fun(); } };