Created
April 26, 2012 05:50
-
-
Save bgaff/2496338 to your computer and use it in GitHub Desktop.
FizzBuzz C++: Template 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
/* | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's actually a super nice example on how to use template recursion. Thanks!