Last active
August 29, 2015 13:57
-
-
Save martinmoene/9485071 to your computer and use it in GitHub Desktop.
with scoped
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
// [std-proposals] Using the underscore for unused identifiers | |
// https://groups.google.com/a/isocpp.org/d/msg/std-proposals/a4CRu2KONZ8/N0aPF76B990J | |
#include <iostream> | |
// _, or ANON | |
#define with( expr ) if ( auto&& _ = (expr) ) | |
struct wither | |
{ | |
operator bool() { return true; } | |
}; | |
struct scoped : wither | |
{ | |
const char c; | |
scoped(char c) :c(c) { std::cout << "scoped(" << c << ")\n"; }; | |
~scoped() { std::cout << "~scoped(" << c << ")\n"; }; | |
scoped( scoped && other ) : c(other.c) { } | |
}; | |
int main() | |
{ | |
with ( scoped('a') ) | |
{ | |
std::cout << "{ block-1 }\n"; | |
with ( scoped('b') ) | |
{ | |
std::cout << "{ block-2 }\n"; | |
} | |
} | |
} | |
// g++ -Wall -std=c++11 -Wno-unused-variable- -o with.exe with.cpp && with | |
// scoped(a) | |
// { block-1 } | |
// scoped(b) | |
// { block-2 } | |
// ~scoped(b) | |
// ~scoped(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment