Skip to content

Instantly share code, notes, and snippets.

@NocturnDragon
Last active October 3, 2017 14:47
Show Gist options
  • Save NocturnDragon/91ad0bb8a57f2cc1d699211a4774b576 to your computer and use it in GitHub Desktop.
Save NocturnDragon/91ad0bb8a57f2cc1d699211a4774b576 to your computer and use it in GitHub Desktop.
With constructor in C++17
#include <cstdio>
#define with(a) if(a ; true)
struct ExampleScopeGuard
{
ExampleScopeGuard () : foo(5)
{
printf("ACQUIRE\n");
}
~ExampleScopeGuard ()
{
printf("RELEASE\n");
}
int foo;
};
int main ()
{
// Prints:
// ACQUIRE
// INSIDE: 5
// RELEASE
with(ExampleScopeGuard guard)
{
printf("\t INSIDE: %d\n", guard.foo);
}
// instead of
{
ExampleScopeGuard guard;
printf("\t INSIDE: %d\n", guard.foo);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment