- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
#include <iostream>
// Truthiness
struct true_type{static const bool value = true;};
struct false_type{ static const bool value = false;};
// Pick type based on bool value
template <bool B, typename T, typename U>struct conditional{typedef T type;};
template <typename T, typename U>struct conditional<false, T, U>{typedef U type;};
// Compare two types
template <typename T, typename U>struct is_same : public false_type{};
template <typename T>struct is_same<T, T> : public true_type{};
// Division predicate
template <int a, int b>struct divides{static const bool value = (a % b == 0);};
// Type traits
template <typename T, int N = 0>
struct fizzbuzz_traits{
typedef T type;
static const int value = N;
};
template <> struct fizzbuzz_traits<char*, 0>{typedef char* type;};
// fizzbuzz type initializers
template <bool A, bool B> struct fizzbuzz{};
template <> struct fizzbuzz<true, false> : public fizzbuzz_traits<char*> {static const char* value;};
typedef fizzbuzz<true, false> fizz_type;
const char* fizz_type::value = "fizz"; // static const char* can only be initialized out of line
template <> struct fizzbuzz<true, true> : public fizzbuzz_traits<char*> {static const char* value;};
typedef fizzbuzz<true, true> fizzbuzz_type;
const char *fizzbuzz_type::value = "fizzbuzz";
template <> struct fizzbuzz<false, true> : public fizzbuzz_traits<char*> {static const char* value;};
typedef fizzbuzz<false, true> buzz_type;
const char *buzz_type::value = "buzz";
template <> struct fizzbuzz<false, false> : fizzbuzz_traits<void>{};
// FizzBuzz solver
template <int N>
class FizzBuzz{
private:
struct is_divisible : public true_type{
struct by3 : public divides<N, 3>{};
struct by5 : public divides<N, 5>{};
};
public:
typedef fizzbuzz< is_divisible::by3::value, is_divisible::by5::value > solution_type;
// stores string or 'false'
typedef fizzbuzz_traits<int, N> non_divisible_type;
// stores number
typedef typename conditional< !is_same<void, typename solution_type::type>::value,
solution_type,
non_divisible_type>::type print_type;
// stores value to print
};
// Looping from N to M
template <int N, int M>
struct static_for{
static void value(){
std::cout << FizzBuzz<N>::print_type::value << std::endl;
static_for<N + 1, M>::value();
}
};
template <>
struct static_for<101, 100>
{
static void value(){};
};
int main(){
static_for<1, 100>::value();
}