finally.hh: delete copy constructor which is a bad idea
This commit is contained in:
@@ -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(); }
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user