File tree Expand file tree Collapse file tree 2 files changed +12
-5
lines changed
Expand file tree Collapse file tree 2 files changed +12
-5
lines changed Original file line number Diff line number Diff line change 44#define BEMAN_SCOPE_HPP
55
66#include < concepts>
7+ #include < functional>
78#include < exception>
89
910namespace beman ::scope {
@@ -78,7 +79,7 @@ struct ExecuteAlways;
7879// =========================================================
7980
8081template <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
187188template <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
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments