From 6b284bf5007e3d10b40eb54c5bf07a19e72b66db Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Tue, 30 Aug 2022 16:05:09 -0700 Subject: [PATCH] Applying Casey's suggestions Applying @CaseyCarter's suggested forwarding changes And adding `[[nodiscard]]` on `finally` Thanks Casey -- somehow this slipped through the cracks for a year. --- include/gsl/util | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/include/gsl/util b/include/gsl/util index 5e9256f..757be6a 100644 --- a/include/gsl/util +++ b/include/gsl/util @@ -52,7 +52,9 @@ template class final_action { public: - explicit final_action(F f_) : f(std::move(f_)) { } + template + explicit final_action(FF&& ff) : f{std::forward(ff)} { } + ~final_action() { if (invoke) f(); } final_action(final_action&& other) @@ -60,8 +62,8 @@ public: { } final_action(const final_action& rhs) = delete; - final_action& operator=(const final_action&) = delete; - final_action& operator=(final_action&& other) = delete; + void operator=(const final_action&) = delete; + void operator=(final_action&& other) = delete; private: F f; @@ -70,9 +72,9 @@ private: // finally() - convenience function to generate a final_action template -auto finally(F f) noexcept +[[nodiscard]] auto finally(F&& f) noexcept { - return final_action{f}; + return final_action{std::forward(f)}; } // narrow_cast(): a searchable way to do narrowing casts of values