Skip to content

Instantly share code, notes, and snippets.

@FinalTheory
Last active May 22, 2017 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FinalTheory/ec4b0da8447e491971f92540b4f0b8b8 to your computer and use it in GitHub Desktop.
Save FinalTheory/ec4b0da8447e491971f92540b4f0b8b8 to your computer and use it in GitHub Desktop.
C++ Template Hanoi
#include <iostream>
#include <utility>
template <typename T, T a, T b>
struct pair {
static constexpr auto first = a;
static constexpr auto second = b;
static void print() { std::cout << first << "->" << second << std::endl; }
};
template <typename... Args>
struct sequence {};
template <typename seq1, typename seq2>
struct concat;
template <typename... Args1, typename... Args2>
struct concat<sequence<Args1...>, sequence<Args2...>> {
using type = sequence<Args1..., Args2...>;
};
template <int, typename T, T a, T b, T c>
struct hanoi;
template <typename T, T a, T b, T c>
struct hanoi<1, T, a, b, c> {
using type = sequence<pair<T, a, c>>;
};
template <int n, typename T, T a, T b, T c>
struct hanoi {
using type = typename concat<
typename concat<typename hanoi<n - 1, T, a, c, b>::type,
sequence<pair<T, a, c>>
>::type,
typename hanoi<n - 1, T, b, a, c>::type
>::type;
};
template <typename... Args>
void print_seq(sequence<Args...>) {
bool Do[] = { (Args::print(), true)... };
(void)Do;
}
int main(int, const char*[]) {
print_seq(typename hanoi<8, char, 'A', 'B', 'C'>::type());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment