Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- const char FIZZ = 1;
- const char BUZZ = 2;
- const char FIZZBUZZ = 3;
- const char NONE = 0;
- template<char fizz_flag, unsigned N>
- struct FBResult;
- // FIZZ
- template<unsigned N> struct FBResult<FIZZ, N>
- {
- enum { size = 5 };
- char data[size];
- constexpr FBResult() : data({ 'F','i','z','z','\n' }) {}
- };
- // BUZZ
- template<unsigned N> struct FBResult<BUZZ, N>
- {
- enum { size = 5 };
- char data[size];
- constexpr FBResult() : data({ 'B','u','z','z','\n' }) {}
- };
- // FIZZBUZZ
- template<unsigned N> struct FBResult<FIZZBUZZ, N>
- {
- enum { size = 9 };
- char data[size];
- constexpr FBResult() : data({ 'F','i','z','z','B','u','z','z','\n' }) {}
- };
- // NUMBER
- template<unsigned N> struct FBResult<NONE, N>
- {
- enum { size = 5 };
- char data[size];
- constexpr FBResult() : data({ N/1000 + 48, N/100 + 48, N/10 + 48, N%10 + 48, '\n' }) {}
- };
- 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
- {
- char data[sizeof...(args)];
- constexpr CompileTimeArray() : data({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;
- for(size_t i = 0; i < sizeof(A().data); ++i)
- {
- std::cout << A().data[i];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment