Skip to content

Instantly share code, notes, and snippets.

@martinmoene
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinmoene/9485071 to your computer and use it in GitHub Desktop.
Save martinmoene/9485071 to your computer and use it in GitHub Desktop.
with scoped
// [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