Skip to content

Instantly share code, notes, and snippets.

@LCamel
Created December 12, 2016 18:55
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 LCamel/9f5305d53d11dfe96d73ebda7c23048e to your computer and use it in GitHub Desktop.
Save LCamel/9f5305d53d11dfe96d73ebda7c23048e to your computer and use it in GitHub Desktop.
Print with error messages
// https://www.ptt.cc/bbs/Programming/M.1481568735.A.6D7.html
// g++ -std=c++0x -ftemplate-depth=1001 a.cpp 2>&1 | grep -oP '(?<=Print<)\d+'
template <int i> struct Print { enum : int {
v = (void) i // die!
};};
template <int i, bool b> struct Test { enum : int {
v = 42
};};
template <int i > struct Test<i, false> { enum : int {
v = 42
};};
template <int i > struct Test<i, true> { enum : int {
v = Print<i>::v
};};
template <int i, int d2, int d1, int d0> struct Digits { enum : int {
v = Test<i, d2 == 0 ? (d1 == 0 || d1 != d0)
: d2 != d1 && d1 != d0 && d0 != d2
>::v
};};
template <int i> struct Loop { enum : int {
p = Digits<i, i / 100, (i / 10) % 10, i % 10>::v,
v = Loop<i + 1>::v
};};
template <> struct Loop<1000> { enum : int {
v = 42
};};
Loop<1> go;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment