Skip to content

Instantly share code, notes, and snippets.

@bgaff
Created April 26, 2012 05:50
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bgaff/2496338 to your computer and use it in GitHub Desktop.
Save bgaff/2496338 to your computer and use it in GitHub Desktop.
FizzBuzz C++: Template Recursion
/*
* fizzbuzz.cpp
*
* Created on: Apr 25, 2012
* Author: Brian Geffon
*
* fizzbuzz solved without looping or conditionals using only template recursion.
*/
#include <iostream>
#include <string>
template <int r> struct FizzBuzzPrinter {
static const int fizzBuzzed = 0;
template <typename T> FizzBuzzPrinter(T t) {}
};
template <> struct FizzBuzzPrinter<0> {
static const int fizzBuzzed = 1;
template <typename T> FizzBuzzPrinter(T t) {
std::cout << t;
}
};
template <int N> struct FizzBuzz: FizzBuzz<N - 1> {
FizzBuzz() {
FizzBuzzPrinter<(N % 15)>("FizzBuzz");
FizzBuzzPrinter<(N % 5) + FizzBuzzPrinter<N % 15>::fizzBuzzed>("Buzz");
FizzBuzzPrinter<(N % 3) + FizzBuzzPrinter<(N % 15)>::fizzBuzzed + FizzBuzzPrinter<(N % 5) + FizzBuzzPrinter<N % 15>::fizzBuzzed>::fizzBuzzed>("Fizz");
FizzBuzzPrinter<FizzBuzzPrinter<N % 3>::fizzBuzzed + FizzBuzzPrinter<N % 5>::fizzBuzzed>(int(N));
std::cout << std::endl;
}
};
template <> struct FizzBuzz<0> {};
int main (int argc, char **argv)
{
FizzBuzz<100> p;
return 0;
}
@grisevg
Copy link

grisevg commented May 24, 2016

That's actually a super nice example on how to use template recursion. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment