Skip to content

Instantly share code, notes, and snippets.

@azenla
Last active June 8, 2017 14:18
Show Gist options
  • Save azenla/a9608f81fb16880a356d00d7643db7c2 to your computer and use it in GitHub Desktop.
Save azenla/a9608f81fb16880a356d00d7643db7c2 to your computer and use it in GitHub Desktop.
Specification Meta-Programming
#define STYPE(type, vt, defcase) template <vt input> struct type { enum { value = (defcase) }; };
#define ITYPE(type, defcase) STYPE(type, int, defcase)
#define SPEC(type, id, result) template <> struct type<id> { enum { value = (result) }; };
#define SVALUE(type, input) type<input>::value
ITYPE(Fib, Fib<input - 1>::value + Fib<input - 2>::value)
SPEC(Fib, 0, 0)
SPEC(Fib, 1, 1)
int main() {
static_assert(SVALUE(Fib, 10) == 55);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment