Created
October 26, 2011 17:14
-
-
Save t-mat/1317043 to your computer and use it in GitHub Desktop.
C++ scoped object trick
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 <stdio.h> | |
class A { | |
public: | |
A() { printf("A : begin\n"); } | |
~A() { printf("A : end\n"); } | |
operator bool() const { return true; } | |
}; | |
#define SCOPED_OBJ() if(A a = A()) | |
int main() { | |
SCOPED_OBJ() { | |
printf("in scope (1)\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment