-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfizzbuzz.cpp
More file actions
60 lines (50 loc) · 1.09 KB
/
fizzbuzz.cpp
File metadata and controls
60 lines (50 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
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
/*
* compile time fizzbuzz
* by Paul Dreik
* http://www.pauldreik.se
*/
#include <iostream>
#include <utility>
#include <string>
#include <typeinfo>
namespace P {
struct Fizz {};
struct Buzz {};
struct FizzBuzz {};
template<int I>
struct Number {};
template<int I, bool D3, bool D5>
struct BoxBase {
using type=Number<I>;
};
template<int I>
struct BoxBase<I, true, false> {
using type=Fizz;
};
template<int I>
struct BoxBase<I, false, true> {
using type=Buzz;
};
template<int I>
struct BoxBase<I, true, true> {
using type=FizzBuzz;
};
template<int I>
struct Box: BoxBase<I, (I % 3) == 0, (I % 5) == 0> {
static inline const char* toChar() {
return typeid(typename Box::type).name();
}
};
template<int ...Indices>
void implementation(std::integer_sequence<int, Indices...>) {
for (auto e : { Box<Indices+1>::toChar()... }) {
std::cout << e << "\n";
}
// to get a tuple containing the fizzbuzz sequence instead:
//auto x=std::tuple<typename Box<Indices+1>::type...>();
}
}
void playFizzBuzz() {
const int N = 898;
P::implementation(std::make_integer_sequence<int, N>());
}