diff options
| author | Maxime Coste <mawww@kakoune.org> | 2018-05-01 22:38:51 +1000 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2018-05-01 22:47:06 +1000 |
| commit | 6777c1469727b27d7909cd1aafee23458ba19431 (patch) | |
| tree | ef7c05f6aa295c2eeb95d5f45ae90f781a228db8 /src/utils.hh | |
| parent | 1fb53ca712de9deef5d79a53522dcd4be7a88ae7 (diff) | |
Make OnScopeEnd valid even when non-copy elided
OnScopeEnd was relying on copy elision to avoid temporary destructor
calls that would run the scope end function too soon.
Diffstat (limited to 'src/utils.hh')
| -rw-r--r-- | src/utils.hh | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/utils.hh b/src/utils.hh index 9eced1c2..23afdeb1 100644 --- a/src/utils.hh +++ b/src/utils.hh @@ -65,11 +65,18 @@ class OnScopeEnd { public: [[gnu::always_inline]] - OnScopeEnd(T func) : m_func(std::move(func)) {} + OnScopeEnd(T func) : m_func{std::move(func)}, m_valid{true} {} [[gnu::always_inline]] - ~OnScopeEnd() { m_func(); } + OnScopeEnd(OnScopeEnd&& other) + : m_func{std::move(other.m_func)}, m_valid{other.m_valid} + { other.m_valid = false; } + + [[gnu::always_inline]] + ~OnScopeEnd() { if (m_valid) m_func(); } + private: + bool m_valid; T m_func; }; |
