diff options
author | jade <lix@jade.fyi> | 2024-03-11 15:11:20 -0600 |
---|---|---|
committer | Gerrit Code Review <gerrit@lix> | 2024-03-11 15:11:20 -0600 |
commit | df2723b972aab036fb19b3ca5ada7ba59c8d15ed (patch) | |
tree | d402744b8e8ef9a87dbbfc4109dfc8ecbd4c40a0 | |
parent | d9367da02780821316b012b34017a35cc60b751a (diff) | |
parent | 45f6e3521a2b4f535e29d03dd077978a691bdb58 (diff) |
Merge "finally.hh: delete copy constructor which is a bad idea" into main
-rw-r--r-- | src/libutil/finally.hh | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/src/libutil/finally.hh b/src/libutil/finally.hh index db654301f..49263ee6d 100644 --- a/src/libutil/finally.hh +++ b/src/libutil/finally.hh @@ -9,8 +9,15 @@ class 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(); } }; |