By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Sorting via template metaprogramming - critique requested

P: n/a

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_3(N1, N2, N3) value_list<(N1), CREATE_2((N2), (N3)) >
#define CREATE_4(N1, N2, N3, N4) value_list<(N1), CREATE_3((N2), (N3), (N4))

#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<N1, CREATE_1(N2) >,
value_list<N2, CREATE_1(N1) >::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
Jul 22 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.