作者:flysnwoxg
c++编译器模板解析确实很强大。基本上可以把编译器看做是一个虚拟机,c++模板源代码就是被执行的脚本代码。
基本上你可以在编译期实现任何算法。
下面是一个按照从小到大,然后从大到小对数值对排序的程序,程序使用了冒泡排序,排序在编译期完成。
例如程序的原始输入为: (
(5,6),
(1,5),
(3,4))
将被从小到大排序为
(
(1,5),
(3,4),(5,6))
然后被从大到小排序为
(
(5,6)
,
(3,4)
,
(1,5)
)
//author:flysnowxg
#include "stdio.h"
//用模板表示类型,模板的实例化表示对象(如pait_t<1,2> 表示(1,2)这样的两个值的对象)
template<int _first,int _second>
struct pair_t{
static const int first=_first;
static const int second=_second;
static void print(){printf("%d %d",first,second);}
};
//计算两个不同的pair_t类型的实例的小于关系
template <typename T1,typename T2>
struct less_t{
static const int first_le=T1::first<T2::first;
static const int first_eq=T1::first==T2::first;
static const int second_le=(first_eq&&(T1::second<T2::second));
static const int result=first_le || second_le;
};
//计算两个不同的pair_t类型的实例的大于关系
template <typename T1,typename T2>
struct greate_t{
static const int result=!less_t<T1,T2>::result;
};
struct null_t;
//类型列表
template <typename T1,typename T2> struct list_t;
template <typename T>
struct list_t<T,null_t>{
typedef T value;
typedef null_t next;
};
template <typename T1,typename T2,typename T3>
struct list_t<T1,list_t<T2,T3> >
{
typedef T1 value;
typedef list_t<T2,T3> next;
};
#define list_t1(e1) list_t<e1,null_t>
#define list_t2(e1,e2) list_t<e1,list_t1(e2)>
#define list_t3(e1,e2,e3) list_t<e1,list_t2(e2,e3)>
#define list_t4(e1,e2,e3,e4) list_t<e1,list_t3(e2,e3,e4)>
#define list_t5(e1,e2,e3,e4,e5) list_t<e1,list_t4(e2,e3,e4,e5)>
#define list_t6(e1,e2,e3,e4,e5,e6) list_t<e1,list_t5(e2,e3,e4,e5,e6)>
#define list_t7(e1,e2,e3,e4,e5,e6,e7) list_t<e1,list_t6(e2,e3,e4,e5,e6,e7)>
#define list_t8(e1,e2,e3,e4,e5,e6,e7,e8) list_t<e1,list_t7(e2,e3,e4,e5,e6,e7,e8)>
#define list_t9(e1,e2,e3,e4,e5,e6,e7,e8,e9) list_t<e1,list_t8(e2,e3,e4,e5,e6,e7,e8,e9)>
#define list_t10(e1,e2,e3,e4,e5,e6,e7,e8,e9,e10) list_t<e1,list_t9(e2,e3,e4,e5,e6,e7,e8,e9,e10)>
//递归打印类型列表中每个类型的值
template<typename T> struct print_t;
template<typename T>
struct print_t<list_t<T,null_t>>
{
typedef typename T result;
static void print(){
printf("\nelem::");
result::print();
}
};
template<typename T1,typename T2>
struct print_t<list_t<T1,T2>>
{
typedef typename T1 result;
static void print(){
printf("\nelem::");
result::print();
print_t<T2>::print();
}
};
//冒泡排序算法
template<typename T,template <typename,typename> class CompareT> struct sort_t;
template<typename T,template <typename,typename> class CompareT>
struct sort_t<list_t<T,null_t> ,CompareT>{
typedef list_t<T,null_t> sort_head;
typedef T least_elem;
typedef null_t remainder;
typedef list_t<T,null_t> result;
};
template<typename T1,typename T2,template <typename,typename> class CompareT>
struct sort_t<list_t<T1,T2>,CompareT>{
template<bool _b_swap> struct swap_t{
typedef list_t<T1,T2> result;
};
template<> struct swap_t<false>{
typedef list_t<typename T2::value,list_t<T1,typename T2::next> > result;
};
static const int order=!CompareT<T1,T2::value>::result;
typedef typename swap_t<order>::result sort_head;//假如CompareT是less_t,将开头两个元素中大的放前面,小的放后面
typedef typename sort_t<typename sort_head::next,CompareT>::least_elem least_elem;//假如CompareT是less_t,获取列表中最小的元素
typedef list_t<typename sort_head::value,typename sort_t<typename sort_head::next,CompareT>::remainder> remainder;//去掉末尾那个最小元素
typedef list_t<least_elem,typename sort_t<remainder,CompareT>::result> result;//将最小元素和剩余已经排好序的元素链表组成一个新链表
};
int main(int argc, char* argv[])
{
typedef pair_t<50,6> e1_t;
typedef pair_t<9,10> e2_t;
typedef pair_t<1,2> e3_t;
typedef pair_t<7,8> e4_t;
typedef pair_t<3,4> e5_t;
typedef pair_t<-6,4> e6_t;
typedef list_t6(e1_t,e2_t,e3_t,e4_t,e5_t,e6_t) date_t;
printf("原始数据:");
print_t<date_t>::print();
typedef sort_t<date_t,less_t>::result data_ta;
printf("\n\n从小到大:");
print_t<data_ta>::print();
typedef sort_t<date_t,greate_t>::result data_tb;
printf("\n\n从大到小:");
print_t<data_tb>::print();
}
最后的输出:
原始数据:
elem::50 6
elem::9 10
elem::1 2
elem::7 8
elem::3 4
elem::-6 4
从小到大:
elem::-6 4
elem::1 2
elem::3 4
elem::7 8
elem::9 10
elem::50 6
从大到小:
elem::50 6
elem::9 10
elem::7 8
elem::3 4
elem::1 2
elem::-6 4