Skip to content

Instantly share code, notes, and snippets.

@lomereiter
Created November 29, 2011 23:13
Show Gist options
  • Save lomereiter/1407085 to your computer and use it in GitHub Desktop.
Save lomereiter/1407085 to your computer and use it in GitHub Desktop.
flattening (C++11)
template <typename... T> struct type {};
struct A{}; struct B{}; struct C{}; struct D{}; struct E{}; struct F{};
struct G{}; struct H{}; struct I{}; struct J{}; struct K{}; struct L{};
struct M{}; struct N{}; struct O{}; struct P{};
typedef type<A, B, type<C, D>, type<E, F, type<G, H>, I, J>,
type<type<type<K, type<L>>, M>, N>, O, P> init;
typedef type<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P> expected;
template <typename... T> struct cons;
template <typename H, typename... T>
struct cons<H, type<T...>> {
typedef ::type<H, T...> type;
};
template <typename... T> struct flatten;
template <> struct flatten<type<>> { typedef ::type<> type; };
template <typename... T, typename... U>
struct flatten<type<type<T...>, U...>> {
typedef typename flatten< ::type<T..., U...>>::type type;
};
template <typename H, typename... T>
struct flatten<type<H, T...>> {
typedef typename cons<H, typename flatten< ::type<T...>>::type>::type type;
};
#include <typeinfo>
#include <iostream>
int main() {
std::cout << typeid(init).name() << std::endl;
std::cout << typeid(flatten<init>::type).name() << std::endl;
std::cout << typeid(expected).name() << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment