Skip to content

Instantly share code, notes, and snippets.

@elbeno
Created January 6, 2016 05:06
Show Gist options
  • Save elbeno/ebf34262b588d66459b4 to your computer and use it in GitHub Desktop.
Save elbeno/ebf34262b588d66459b4 to your computer and use it in GitHub Desktop.
FizzBuzz in C++14
#include <cstddef>
#include <iostream>
#include <string>
#include <utility>
using namespace std;
template <bool div3, bool div5>
struct printer
{
void operator()(int n)
{
cout << to_string(n);
}
};
template <>
struct printer<true, false>
{
void operator()(int)
{
cout << "Fizz";
}
};
template <>
struct printer<false, true>
{
void operator()(int)
{
cout << "Buzz";
}
};
template <>
struct printer<true, true>
{
void operator()(int)
{
printer<true, false>()(0);
printer<false, true>()(0);
}
};
template <size_t ...Is>
void fizzbuzz(index_sequence<Is...>)
{
using I = initializer_list<int>;
(void) I { (printer<(Is+1) % 3 == 0, (Is+1) % 5 == 0>()(Is+1), cout << endl, 0)... };
}
int main(void)
{
fizzbuzz(make_index_sequence<100>());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment