Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
C++ Meta-Programming Tree Structure
#include <iostream>
struct Nix {};
template<int val, typename left, typename right>
struct Was
{
using left_type = left;
using right_type = right;
static const int value = val;
};
template<int n>
struct maketree
{
using type = Was<n, typename maketree<n - 1>::type, typename maketree<n - 1>::type>;
};
template<>
struct maketree<0>
{
using type = Nix;
};
using testtree = maketree<10>::type;
template<typename tree>
struct treestr
{
static void print()
{
std::cout << "(";
treestr<typename tree::left_type>::print();
std::cout << "|" << tree::value << "|";
treestr<typename tree::right_type>::print();
std::cout << ")";
}
};
template<>
struct treestr<Nix>
{
static void print()
{
std::cout << "nix";
}
};
int main(int argc, char const *argv[])
{
treestr<testtree>::print();
std::cout << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment