Skip to content

Commit df58812

Browse files
change inheritance to member variable
1 parent e143951 commit df58812

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

include/beman/scope/scope.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define BEMAN_SCOPE_HPP
55

66
#include <concepts>
7+
#include <functional>
78
#include <exception>
89

910
namespace beman::scope {
@@ -78,7 +79,7 @@ struct ExecuteAlways;
7879
//=========================================================
7980

8081
template <invocable_return<void> ExitFunc, scope_invoke_checker InvokeChecker = ExecuteAlways>
81-
class scope_guard : public InvokeChecker {
82+
class scope_guard {
8283
public:
8384
explicit scope_guard(ExitFunc&& exit_func) /*noexcept(see below)*/
8485
: m_exit_func(std::move(exit_func)) //
@@ -185,17 +186,19 @@ class Releasable<void> {
185186
};
186187

187188
template <scope_invoke_checker InvokeChecker>
188-
class Releasable<InvokeChecker> : private InvokeChecker {
189+
class Releasable<InvokeChecker> {
189190
public:
190191
Releasable() = default;
191192

192193
Releasable(InvokeChecker&& invoke_checker) : InvokeChecker(std::move(invoke_checker)) {}
193194

194-
bool can_invoke() const { return m_can_invoke && static_cast<const InvokeChecker*>(this)->can_invoke(); }
195+
bool can_invoke() const { return m_can_invoke && m_invoke_checker.can_invoke(); }
195196

196197
void release() { m_can_invoke = false; }
197198

198199
private:
200+
[[no_unique_address]] InvokeChecker m_invoke_checker = {};
201+
199202
bool m_can_invoke = true;
200203
};
201204

tests/beman/scope/scope.test.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ TEST_CASE("scope_guard") {
1414
SECTION("Constructing") {
1515

1616
auto exit_guard = beman::scope::scope_guard{[] {}, [] { return true; }};
17-
// exit_guard.release(); // doesn't compile (as planned)- NOT releasable
17+
// vvv doesn't compile (as planned)- NOT releasable
18+
// exit_guard.release();
1819

19-
// auto exit_guard2 = beman::scope::scope_guard{[] {}, [] { return 1; }}; // Doesn't compile, return must be bool
20+
REQUIRE(sizeof(decltype(exit_guard)) == 1);
21+
22+
// vvv Doesn't compile (as planned), return must be bool
23+
// auto exit_guard2 = beman::scope::scope_guard{[] {}, [] { return 1; }};
2024

2125
REQUIRE(true);
2226
}

0 commit comments

Comments
 (0)