#include <iostream>
const char FIZZ = 1;
const char BUZZ = 2;
const char FIZZBUZZ = 3;
const char NONE = 0;
template<char Flag, unsigned Index>
struct FBResult;
// FIZZ
template<unsigned Index> struct FBResult<FIZZ, Index>
{
enum { size = 5 };
static const char data[size];
};
template<unsigned Index>
const char FBResult<FIZZ, Index>::data[FBResult<FIZZ, Index>::size] = { 'F','i','z','z','\n' };
// BUZZ
template<unsigned Index> struct FBResult<BUZZ, Index>
{
enum { size = 5 };
static const char data[size];
};
template<unsigned Index>
const char FBResult<BUZZ, Index>::data[FBResult<BUZZ, Index>::size] = { 'B','u','z','z','\n' };
// FIZZBUZZ
template<unsigned Index> struct FBResult<FIZZBUZZ, Index>
{
enum { size = 9 };
static const char data[size];
};
template<unsigned Index>
const char FBResult<FIZZBUZZ, Index>::data[FBResult<FIZZBUZZ, Index>::size] = { 'F','i','z','z','B','u','z','z','\n' };
// NUMBER
template<unsigned Index> struct FBResult<NONE, Index>
{
enum { size = 5 };
static const char data[size];
};
template<unsigned Index>
const char FBResult<NONE, Index>::data[FBResult<NONE, Index>::size] = { Index/1000 + 48, (Index%1000)/100 + 48, (Index%100)/10 + 48, (Index%10) + 48, '\n' };
// this sets value to a flag value of either 1, 2, 3, or 0,
// determining whether to print fizz, buzz, fizzbuzz, or number
template <unsigned N> struct FizzFlag
{
enum { value = (0x0000 | ((N % 3 == 0) * FIZZ) | ((N % 5 == 0) * BUZZ)) };
};
template<unsigned N, unsigned c> struct FizzBuzzChar
{
enum { value = FBResult<FizzFlag<N>::value, N>::data[c] };
};
template<unsigned N>
struct FizzStringSize
{
enum { value = FBResult<FizzFlag<N>::value, N>::size };
};
template<char... args>
struct CompileTimeArray
{
static const char data[sizeof...(args)];
};
template <char... args>
const char CompileTimeArray<args...>::data[sizeof...(args)] = {args...};
template<unsigned C, unsigned N, char... args>
struct generate_array_impl
{
typedef typename generate_array_impl<C-1, N, FizzBuzzChar<N,C>::value, args...>::result result;
};
template<unsigned N, char... args>
struct generate_array_impl<0, N, args...>
{
typedef typename generate_array_impl<FizzStringSize<N-1>::value - 1, N-1, FizzBuzzChar<N,0>::value, args...>::result result;
};
template<char... args>
struct generate_array_impl<0, 1, args...>
{
typedef CompileTimeArray< FizzBuzzChar<1,0>::value, args...> result;
};
template<unsigned N>
struct GenerateFizzBuzzArray
{
typedef typename generate_array_impl<FizzStringSize<N-1>::value - 1, N-1, '\0'>::result result;
};
int main(void)
{
const unsigned count = 100;
typedef GenerateFizzBuzzArray<count>::result A;
std::cout << A::data;
return 0;
}