diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/gsl/util | 35 |
1 files changed, 12 insertions, 23 deletions
diff --git a/include/gsl/util b/include/gsl/util index aebfc66..75c78ad 100644 --- a/include/gsl/util +++ b/include/gsl/util @@ -65,40 +65,29 @@ template <class F> class final_action { public: - static_assert(!std::is_reference<F>::value && !std::is_const<F>::value && - !std::is_volatile<F>::value, - "Final_action should store its callable by value"); + explicit final_action(const F& ff) noexcept : f{ff} { } + explicit final_action(F&& ff) noexcept : f{std::move(ff)} { } - explicit final_action(F f) noexcept : f_(std::move(f)) {} + ~final_action() noexcept { if (invoke) f(); } final_action(final_action&& other) noexcept - : f_(std::move(other.f_)), invoke_(std::exchange(other.invoke_, false)) - {} + : f(std::move(other.f)), invoke(std::exchange(other.invoke, false)) + { } - final_action(const final_action&) = delete; - final_action& operator=(const final_action&) = delete; - final_action& operator=(final_action&&) = delete; - - // clang-format off - GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // terminate if throws - // clang-format on - ~final_action() noexcept - { - if (invoke_) f_(); - } + final_action(const final_action&) = delete; + void operator=(const final_action&) = delete; + void operator=(final_action&&) = delete; private: - F f_; - bool invoke_{true}; + F f; + bool invoke = true; }; // finally() - convenience function to generate a final_action template <class F> -GSL_NODISCARD final_action<typename std::remove_cv<typename std::remove_reference<F>::type>::type> -finally(F&& f) noexcept +GSL_NODISCARD auto finally(F&& f) noexcept { - return final_action<typename std::remove_cv<typename std::remove_reference<F>::type>::type>( - std::forward<F>(f)); + return final_action<std::decay_t<F>>{std::forward<F>(f)}; } // narrow_cast(): a searchable way to do narrowing casts of values |