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.
This commit is contained in:
Casey Carter 2022-09-29 08:59:05 -07:00 committed by GitHub
parent 8840d87199
commit d9ffa118c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -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

View File

@ -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;