Skip to content

Commit

Permalink
Move use of scope guard into constexpr function (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmoene committed Aug 17, 2022
1 parent 8bfb1c5 commit 440b56b
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions test/scope.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,37 @@ void success()
namespace cexpr {

scope_constexpr_ext
void exit()
bool is_called_exit()
{
is_called = true;
bool result = false;
{
auto change = nonstd::make_scope_exit( [&]{ result = true; });
}
return result;
}

scope_constexpr_ext
void fail()
bool is_called_fail()
{
is_called = true;
bool result = false;
try
{
auto change = nonstd::make_scope_fail( [&]{ result = true; });
// throw std::exception();
}
catch(...) {}
return result;
}

scope_constexpr_ext
void success()
bool is_called_success()
{
is_called = true;
bool result = false;
{
auto guard = make_scope_success( [&](){ result = true; } );
// throw std::exception();
}
return result;
}

} // namespace cexpr
Expand Down Expand Up @@ -102,15 +118,7 @@ CASE( "scope_exit: exit function is called at end of scope (lambda)" )
CASE( "scope_exit: exit function is called at end of scope (constexpr)" " [extension]" )
{
#if scope_CPP11_OR_GREATER
is_called = false;

// scope:
{
auto guard = make_scope_exit( cexpr::exit );
// auto guard = make_scope_exit( [](){ is_called = true; } );
}

EXPECT( is_called );
EXPECT( cexpr::is_called_exit() );
#else
EXPECT( !!"Test for constexpr scope_exit not suitable for C++98." );
#endif
Expand Down Expand Up @@ -208,16 +216,7 @@ CASE( "scope_fail: exit function is not called when no exception occurs" )
CASE( "scope_fail: exit function is not called when no exception occurs (constexpr)" " [extension]" )
{
#if scope_CPP11_OR_GREATER
is_called = false;

try
{
auto guard = make_scope_fail( cexpr::fail );
// throw std::exception();
}
catch(...) {}

EXPECT_NOT( is_called );
EXPECT_NOT( cexpr::is_called_fail() );
#else
EXPECT( !!"Test for constexpr scope_fail not suitable for C++98." );
#endif
Expand Down Expand Up @@ -282,16 +281,7 @@ CASE( "scope_success: exit function is called when no exception occurs (lambda)"
CASE( "scope_success: exit function is called when no exception occurs (constexpr)" " [extension]" )
{
#if scope_CPP11_OR_GREATER
is_called = false;

try
{
auto guard = make_scope_success( cexpr::success );
// throw std::exception();
}
catch(...) {}

EXPECT( is_called );
EXPECT( cexpr::is_called_success() );
#else
EXPECT( !!"Test for constexpr scope_success not suitable for C++98." );
#endif
Expand Down

0 comments on commit 440b56b

Please sign in to comment.