diff --git a/include/gsl/util b/include/gsl/util index 2d67b7f..9419e97 100644 --- a/include/gsl/util +++ b/include/gsl/util @@ -52,40 +52,21 @@ template class final_action { public: - static_assert(!std::is_reference::value && !std::is_const::value && - !std::is_volatile::value, - "Final_action should store its callable by value"); - - explicit final_action(F f) noexcept : f_(std::move(f)) {} - - final_action(final_action&& other) noexcept - : f_(std::move(other.f_)), invoke_(std::exchange(other.invoke_, false)) - {} + explicit final_action(F f) : f_(std::move(f)) { } + ~final_action() noexcept { f_(); } 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_(); - } private: F f_; - bool invoke_{true}; }; // finally() - convenience function to generate a final_action template -GSL_NODISCARD final_action::type>::type> -finally(F&& f) noexcept +auto finally(F f) noexcept { - return final_action::type>::type>( - std::forward(f)); + return final_action{f}; } // narrow_cast(): a searchable way to do narrowing casts of values