Skip to content

Instantly share code, notes, and snippets.

@unvBell
Last active August 29, 2015 14:25
Show Gist options
  • Save unvBell/5f4fb2fc9e8c9e11ce13 to your computer and use it in GitHub Desktop.
Save unvBell/5f4fb2fc9e8c9e11ce13 to your computer and use it in GitHub Desktop.
#include <cstdio>
template <char... C>
struct S {
static const char* value() {
static constexpr char s[] = { C..., '\0' };
return s;
}
};
template <typename T, typename U>
struct Plus;
template <char... C, char... D>
struct Plus<S<C...>, S<D...>> {
using Type = S<C..., D...>;
};
template <int N, char... C>
struct ToStr {
using Type = typename ToStr<N/10, N%10+'0', C...>::Type;
};
template <char... C>
struct ToStr<0, C...> {
using Type = S<C...>;
};
template <int I, int M = I%3, int N = I%5>
struct Value {
using Type = typename ToStr<I>::Type;
};
template <int I, int N>
struct Value<I, 0, N> {
using Type = S<'F','i','z','z'>;
};
template <int I, int M>
struct Value<I, M, 0> {
using Type = S<'B','u','z','z'>;
};
template <int I>
struct Value<I, 0, 0> {
using Type = S<'F','i','z','z','B','u','z','z'>;
};
template <int N>
struct FizzBuzz {
using Type = typename Plus<
typename FizzBuzz<N-1>::Type,
typename Plus<
typename Value<N>::Type,
S<'\n'>
>::Type
>::Type;
};
template <>
struct FizzBuzz<0> {
using Type = S<>;
};
int main() {
puts(FizzBuzz<50>::Type::value());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment