Sorting via template metaprogramming - critique requested

D

Dave

For those who might be so inclined, I was wondering if I might get honest
critiques of my first real venture into template metaprogramming. This
template metaprogram sorts a list of integers at compile time. It works,
but if my approach is more awkward or "bad" than need be, I'd love to hear
suggestions for improvement!



#include <iostream>

struct nil {};

template<int N, typename T>
struct value_list
{
};

#define CREATE_1(N1) value_list<(N1), nil>
#define CREATE_2(N1, N2) value_list<(N1), CREATE_1((N2)) >

#define CREATE_5(N1, N2, N3, N4, N5) value_list<(N1),\
CREATE_4((N2), (N3), (N4), (N5)) >

#define CREATE_6(N1, N2, N3, N4, N5, N6) value_list<(N1),\
CREATE_5((N2), (N3), (N4), (N5), (N6)) >

#define CREATE_7(N1, N2, N3, N4, N5, N6, N7) value_list<(N1),\
CREATE_6((N2), (N3), (N4), (N5), (N6), (N7)) >

#define CREATE_8(N1, N2, N3, N4, N5, N6, N7, N8) value_list<(N1),\
CREATE_7((N2), (N3), (N4), (N5), (N6), (N7), (N8)) >

#define CREATE_9(N1, N2, N3, N4, N5, N6, N7, N8, N9) value_list<(N1),\
CREATE_8((N2), (N3), (N4), (N5), (N6), (N7), (N8), (N9)) >

#define CREATE_10(N1, N2, N3, N4, N5, N6, N7, N8, N9, N10) value_list<(N1),\
CREATE_9((N2), (N3), (N4), (N5), (N6), (N7), (N8), (N9), (N10)) >

template<bool B, typename T1, typename T2>
struct type_selector
{
typedef T1 RESULT;
};

template<typename T1, typename T2>
struct type_selector<false, T1, T2>
{
typedef T2 RESULT;
};

template<typename T>
struct print;

template<int N>
struct print<value_list<N, nil> >
{
static void print_it()
{
std::cout << N;
}
};

template<int N, typename T>
struct print<value_list<N, T> >
{
static void print_it()
{
std::cout << N << " ";
print<T>::print_it();
}
};

template<int N, typename T>
struct meta_insert;

template<int N1, int N2>
struct meta_insert<N1, value_list<N2, nil> >
{
typedef typename type_selector<
(N1 < N2),
value_list said:
::RESULT RESULT;
};

template<int N1, int N2, typename T>
struct meta_insert<N1, value_list<N2, T> >
{
typedef typename type_selector<
(N1 < N2),
value_list<N1, value_list<N2, T> >,
value_list<N2, typename meta_insert<N1,
T>::RESULT>
::RESULT RESULT;
};

template<typename T>
struct meta_sort;

template<int N>
struct meta_sort<value_list<N, nil> >
{
typedef value_list<N, nil> RESULT;
};

template<int N, typename T>
struct meta_sort<value_list<N, T> >
{
typedef typename meta_insert<N, typename meta_sort<T>::RESULT>::RESULT
RESULT;
};

int main()
{
print<
meta_sort<
CREATE_10(45, 1, 78, 46, 23, 34, 108, 14, 15, 22)
::RESULT
::print_it();

std::cout << std::endl;
} // main
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

wimax interleaver 0
Translater + module + tkinter 1
error correcting 9
PHDL a new HDL for PCB design 14
c++0x variadic templates 3
Python serial data aquisition 13
Metaprogramming Problem 6
struct array within struct 11

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top