Skip to content

Instantly share code, notes, and snippets.

@farseerfc
Last active August 29, 2015 14:05
Show Gist options
  • Select an option

  • Save farseerfc/b23aca5c2a262a7f30f5 to your computer and use it in GitHub Desktop.

Select an option

Save farseerfc/b23aca5c2a262a7f30f5 to your computer and use it in GitHub Desktop.
#!//usr/bin/env clang++ --std=c++11 -ferror-limit=256 -ftemplate-backtrace-limit=1 -fno-show-column -fno-show-source-location -fno-caret-diagnostics -fno-diagnostics-show-option -fcolor-diagnostics
// run in shell with:
// ./life.cpp 2>&1 | grep error
#include <type_traits>
#define CAT(a,b) CAT1(a,b)
#define CAT1(a,b) a##b
#define say(msg) \
template<int i> struct CAT(Say,__LINE__) \
{ static_assert(i!=i, msg); }; \
CAT(Say,__LINE__)<0> CAT(s,__LINE__)
template<int S, int A = 16807UL, int M = (1UL<<31)-1>
struct Random {
static const int value = ((long)S * A) % M;
typedef Random<value> next;
};
template <typename random, bool success> struct Try{
say("Come On!"); // 摔了,爬起来继续。
Try<typename random::next, random::value % 2> again;
};
template <typename random> struct Try<random, true> {
say("What a beautiful day!");
};
template <typename random> struct Plan{
say("I have something to do!");
Try<random, random::value % 2> doIt; // 想了,就去做。
};
template <int nowage, int age> struct Life{
Plan<Random<nowage>> plan;
Life<nowage+1,age> life; // 赢了,还要再往前走。
};
template <int age> struct Life<age, age>{
say("No regrets."); // 死了,没留下任何遗憾。
};
template struct Life<25, 80>;
// 生命是一段漫长的旅程。
// 想了,就去做。
// 输了,从头再来。
// 摔了,爬起来继续。
// 赢了,还要再往前走。
// 死了,没留下任何遗憾。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment