Merge "finally.hh: delete copy constructor which is a bad idea" into main
This commit is contained in:
commit
df2723b972
1 changed files with 8 additions and 1 deletions
|
@ -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(); }
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue