Last active
April 3, 2019 20:44
-
-
Save Nekrolm/a9cc5426cc3bda1d9c895a7636a7b281 to your computer and use it in GitHub Desktop.
FizzBuzz on variadic templates & recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <utility> | |
namespace impl{ | |
template <int N, int D, int R> | |
constexpr auto FizzBuzz = ""; | |
template <int N> | |
constexpr auto FizzBuzz<N, 15, 0> = "fizzbuzz"; | |
template <int N, int R> | |
constexpr auto FizzBuzz<N, 15, R> = FizzBuzz<N, 5, N % 5>; | |
template <int N> | |
constexpr auto FizzBuzz<N, 5, 0> = "buzz"; | |
template <int N, int R> | |
constexpr auto FizzBuzz<N, 5, R> = FizzBuzz<N, 3, N % 3>; | |
template <int N> | |
constexpr auto FizzBuzz<N, 3, 0> = "fizz"; | |
template <int N, int R> | |
constexpr auto FizzBuzz<N, 3, R> = N; | |
} | |
template <int N> | |
constexpr auto FizzBuzz = impl::FizzBuzz<N, 15, N % 15>; | |
template <int ...Seq> | |
void printFizzBuzz(std::integer_sequence<int, Seq...>){ | |
((std:: cout << FizzBuzz<1 + Seq> << "\n"), ...); | |
} | |
template <int N> | |
void FizzBuzzN(){ | |
printFizzBuzz(std::make_integer_sequence<int, N>{}); | |
} | |
int main() | |
{ | |
FizzBuzzN<30>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment