Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/microsoft/GSL.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCasey Carter <Casey@Carter.net>2022-09-29 18:59:05 +0300
committerGitHub <noreply@github.com>2022-09-29 18:59:05 +0300
commitd9ffa118c50b3fdc43919acb97c3f7001b53971e (patch)
tree228ded88d8b9f6a21b49c4ba2536e37808324d32
parent8840d871995e06e36ac7b2c1f3a1d1a9c447365f (diff)
Properly handle finally(actual_function) (#1056)final_action-revision
`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.
-rw-r--r--include/gsl/util2
-rw-r--r--tests/utils_tests.cpp10
2 files changed, 11 insertions, 1 deletions
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 <class F>
GSL_NODISCARD auto finally(F&& f) noexcept
{
- return final_action<std::remove_cv_t<std::remove_reference_t<F>>>{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
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;