Merge "finally.hh: delete copy constructor which is a bad idea" into main

This commit is contained in:
jade 2024-03-11 15:11:20 -06:00 committed by Gerrit Code Review
commit df2723b972

View file

@ -9,8 +9,15 @@ class Finally
{ {
private: private:
Fn fun; Fn fun;
bool movedFrom = false;
public: public:
Finally(Fn fun) : fun(std::move(fun)) { } 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(); }
}; };