One of C++'s strengths is automatic code invocation at scope-exit (aka RAII). The traditional idea behind RAII of having specialized types for every resource type with a custom destructor that frees it is good in theory but in practice comes with a lot of friction and thus boiler-plate and frustration for the developer, that now has to create a new type for every little rollback operation he wants to perform. The problem is exacerbated with C++11 move operators: making a small "smart" class that does some form of cleanup now requires even more boilerplate.
Fortunately there's a way to neatly work around this, and the idea comes from D's scope
keyword or Go's defer
. We want a mechanism that tells the compiler to invoke a snippet of inlined code at scope exit. Something like
// Acquire a resource
Resource* resource = AcquireResource();
// Then whatever happens, release the resource when it goes out of scope.