Last active
October 3, 2017 14:47
-
-
Save NocturnDragon/91ad0bb8a57f2cc1d699211a4774b576 to your computer and use it in GitHub Desktop.
With constructor in C++17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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