From d9ffa118c50b3fdc43919acb97c3f7001b53971e Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Thu, 29 Sep 2022 08:59:05 -0700 Subject: [PATCH] Properly handle finally(actual_function) (#1056) `finally` needs to use `decay_t` instead of `remove_cvref_t` so it can properly accept non-object function arguments by decaying to function pointer type. Adds test coverage for this use case which was previously missing. --- include/gsl/util | 2 +- tests/utils_tests.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/include/gsl/util b/include/gsl/util index db6a85e..75c78ad 100644 --- a/include/gsl/util +++ b/include/gsl/util @@ -87,7 +87,7 @@ private: template GSL_NODISCARD auto finally(F&& f) noexcept { - return final_action>>{std::forward(f)}; + return final_action>{std::forward(f)}; } // narrow_cast(): a searchable way to do narrowing casts of values diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp index 715073f..7e1e77d 100644 --- a/tests/utils_tests.cpp +++ b/tests/utils_tests.cpp @@ -112,6 +112,16 @@ TEST(utils_tests, finally_function_ptr) EXPECT_TRUE(j == 1); } +TEST(utils_tests, finally_function) +{ + j = 0; + { + auto _ = finally(g); + EXPECT_TRUE(j == 0); + } + EXPECT_TRUE(j == 1); +} + TEST(utils_tests, narrow_cast) { int n = 120;