scope-cpp11 is an implementation of scope_exit
, scope_success
, scope_fail
and unique_resource
proposed for the C++ standard at by P0052r10.
scope_exit
, unique_resource
is available in C++11 or later. scope_success
and scope_fail
are available in C++17 or later (because these require std::uncaught_exceptions()).
#include <scope/scope.hpp>
using namespace scope;
void demo_scope_exit_fail_success_in_p0052() {
std::ostringstream out{};
auto lam = [&]{out << "called ";};
try {
auto v = scope_exit([&]{out << "always ";});
auto w = scope_success([&]{out << "not ";}); // not called
auto x = scope_fail(lam); // called
throw 42;
} catch(...) { // v, x called at this point
auto y = scope_fail([&]{out << "not ";}); // not called
auto z = scope_success([&]{out << "handled";}); // called
} // z called at this point
assert("called always handled" == out.str());
}
void demonstrate_unique_resource_with_stdio_in_p0052() {
auto fclose = [](auto file){ ::fclose(file); }; // not allowed to take address
const std::string filename = "hello.txt";
{ auto file = scope::make_unique_resource(::fopen(filename.c_str(), "w"), fclose);
::fputs("Hello World!\n", file.get());
assert(file.get() != NULL);
}
{ std::ifstream input { filename };
std::string line { };
getline(input, line);
assert("Hello World!" == line);
getline(input, line);
assert(input.eof());
}
::remove(filename.c_str());
{ auto file = scope::make_unique_resource_checked(::fopen("nonexistingfile.txt", "r"), (FILE*)NULL, fclose);
assert(file.get() == NULL);
}
}
scope-cpp11 is a single header-only library. Copy scope.hpp
into your project.
scope-cpp11 depends on the C++ standard library only. (I use catch2 for unit test)
Refer to P0052r10 for detailed documentation.
- Header file name is
scope.hpp
instead of<scope>
. - Namespace is
scope::
instead ofstd::
. - Added
make_scope_exit()
,make_scope_success()
,make_scope_fail()
,make_unique_resource()
functions. This extension is for C++11/14 that doesn't have a template deduction guide.
Scope guard concept was proposed by Petru Marginean and Andrei Alexandrescu.
It has been proposed for standardization by P0052r10, and adopted in 2019-03.
scope-cpp11 passed the test included in the original proposal.