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
path: root/tests
diff options
context:
space:
mode:
authorHerb Sutter <herb.sutter@gmail.com>2022-10-11 02:09:21 +0300
committerGitHub <noreply@github.com>2022-10-11 02:09:21 +0300
commit7d49d4b45d2cc37171351268c6cce43c32dc28a0 (patch)
treebd3e905dca9e76d1993eab46863202e9e18e97f1 /tests
parent849f7ceaf7876286aa663b4f0157b4542090fe90 (diff)
Clean up `final_act` and `finally`, closes #846 (#977)
Somewhere along the way, GSL's implementation of final_act and finally seems to have become way overthought. This PR is to re-simplify these facilities back to what C++ Core Guidelines C.30 said which is simple and clear and works. It just copies the invocable thing, and doesn't bother trying to optimize the copy. This should be fine, because we're typically passing something that's cheap to copy, often a stateless lambda. The problem in #846 appears to be because finally looks like was originally written as a const&/&& overload (its state at the time that issue was opened)... to eliminate a copy when you invoke it with a temporary. If so, then the && was probably never intended to be a forwarder, but an rvalue reference that tripped over the horrid C++ syntax collision where a && parameter magically instead means a forwarding reference because the type happens to be a template parameter type here. So I suspect the original author was just trying to write an rvalue overload, and the forwarder that's there now was never intended at all.
Diffstat (limited to 'tests')
-rw-r--r--tests/utils_tests.cpp10
1 files changed, 10 insertions, 0 deletions
diff --git a/tests/utils_tests.cpp b/tests/utils_tests.cpp
index 0658263..5b64e20 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;