473,852 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting via template metaprogramming - critique requested

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<typena me T1, typename T2>
struct type_selector<f alse, T1, T2>
typedef T2 RESULT;

template<typena me T>
struct print;

template<int N>
struct print<value_lis t<N, nil> >
static void print_it()
std::cout << N;

template<int N, typename T>
struct print<value_lis t<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,

template<typena me 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>::R ESULT>::RESULT

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

std::cout << std::endl;
} // main
Jul 22 '05 #1
0 1260

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Dave | last post by:
Would people agree with the statement that to a large degree, using template metaprogramming techniques turns a C++ compiler into a C++ interpreter (but just for the metaprogrammed portions of the code)? It's not a perfect analogy, but it seems to be a reasonable statement...
by: Mohammad | last post by:
Hi, Is it possible to disable a method of a template class depending on the typename at compile time? thanks!
by: Protoman | last post by:
I've been looking at template metaprogramming. It seems really cool, make the compiler do most of the work. I have very simple program that uses TMP,it calculates the square of a number, but it doesn't seem to work. Here it is: #include <iostream> #include <cstdlib> using namespace std; template<int n>
by: Mark Stijnman | last post by:
I am trying to teach myself template metaprogramming and I have been trying to create lists of related types. I am however stuck when I want to make a template that gives me the last type in a list. I started by using a linked list of types with templates like: struct MyClass1 {}; struct MyClass2 {}; struct MyClass3 {}; struct NullType {};
by: Joe | last post by:
Hi, I found a concept named template metaprogramming that can be used in C+ + code at compile-time. I am a beginner at C++. But I am a programmer on the .NET platform. Do you know if template metaprogramming is supported in C# (.NET)? For reference I found it: http://en.wikipedia.org/wiki/Template_metaprogramming. Thanks to all.
by: Ted | last post by:
I have cross posted this to comp.lang.c++ and to sci.math.num- analysis in the belief that the topic is of interest to some in both groups. I am building my toolkit, in support of my efforts in producing high performance C++ code; code that is provably correct. The issue here is orthogonal to the question of expression templates, which at present seem to me to be relatively simple.
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can anyone explain why loop unrolling doesn't play well with templates? Or better, can someone submit a code fragment to get desired results? Here are the command-lines I use to generate code: "g++ -DTEMPLATE=0 -o gotofun0 gotofun.cpp" works exactly as...
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey Gurtovoy. I’m not doing this because I want to be a Meta Programming guru (because a lot of that stuff looks too crazy for use in the real world). Rather I want to learn heavyweight templates and this is the only
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.