在使用C++编程的过程中, 有时候,我们可能会遇到需要构造很多重复性的参数的地方
比如
int p1, int p2, int p3, int p4
这样的参数序列, 为了方便和简化这种重复性劳动,我在工作中使用了下面这些构造参数表的宏
现在共享出来希望对大家有所帮助
// DP = DEFINE PARAM 定义参数
// STMP = SINGLE TYPE MULTI PARAM 单类型多参数
// 下面的是创建 int p1, int p2, int p3这样形式的参数定义的.
// 比如
// DP_STMP_3(int, p ); 就相当于
// int p1, int p2, int p3
#define DP_STMP_0(type,pname)
#define DP_STMP_1(type,pname) type pname##1
#define DP_STMP_2(type,pname) DP_STMP_1(type,pname), type pname##2
#define DP_STMP_3(type,pname) DP_STMP_2(type,pname), type pname##3
#define DP_STMP_4(type,pname) DP_STMP_3(type,pname), type pname##4
#define DP_STMP_5(type,pname) DP_STMP_4(type,pname), type pname##5
#define DP_STMP_6(type,pname) DP_STMP_5(type,pname), type pname##6
#define DP_STMP_7(type,pname) DP_STMP_6(type,pname), type pname##7
#define DP_STMP_8(type,pname) DP_STMP_7(type,pname), type pname##8
#define DP_STMP_9(type,pname) DP_STMP_8(type,pname), type pname##9
#define DP_STMP_10(type,pname) DP_STMP_9(type,pname), type pname##10
// DP = DEFINE PARAM 定义参数
// DTMP = DOUBLE TYPE MULTI PARAM 两类型多参数
// 范例
// DP_DTMP_3(int, p, float, f ); 就相当于
// int p1, float f1, int p2, float f2, int p3, float f3
#define DP_DTMP_0(type1, pname1, type2, pname2)
#define DP_DTMP_1(type1, pname1, type2, pname2) type1 pname1##1, type2 pname2##1
#define DP_DTMP_2(type1, pname1, type2, pname2) DP_DTMP_1(type1,pname1,type2,pname2), type1 pname1##2, type2 pname2##2
#define DP_DTMP_3(type1, pname1, type2, pname2) DP_DTMP_2(type1,pname1,type2,pname2), type1 pname1##3, type2 pname2##3
#define DP_DTMP_4(type1, pname1, type2, pname2) DP_DTMP_3(type1,pname1,type2,pname2), type1 pname1##4, type2 pname2##4
#define DP_DTMP_5(type1, pname1, type2, pname2) DP_DTMP_4(type1,pname1,type2,pname2), type1 pname1##5, type2 pname2##5
#define DP_DTMP_6(type1, pname1, type2, pname2) DP_DTMP_5(type1,pname1,type2,pname2), type1 pname1##6, type2 pname2##6
#define DP_DTMP_7(type1, pname1, type2, pname2) DP_DTMP_6(type1,pname1,type2,pname2), type1 pname1##7, type2 pname2##7
#define DP_DTMP_8(type1, pname1, type2, pname2) DP_DTMP_7(type1,pname1,type2,pname2), type1 pname1##8, type2 pname2##8
#define DP_DTMP_9(type1, pname1, type2, pname2) DP_DTMP_8(type1,pname1,type2,pname2), type1 pname1##9, type2 pname2##9
#define DP_DTMP_10(type1, pname1, type2, pname2) DP_DTMP_9(type1,pname1,type2,pname2), type1 pname1##10, type2 pname2##10
// DP = DEFINE PARAM 定义参数
// MTMP = MULTI TYPE MULTI PARAM 多类型多参数
// 用来创建诸如 Tp1 p1, Tp2 p2, Tp3 p3 这样形式的参数表
// 比如
// DP_MTMP_2(Tp, p ); 就相当于
// Tp1 p1, Tp2 p2
#define DP_MTMP_0(type,name)
#define DP_MTMP_1(type,name) type##1 name##1
#define DP_MTMP_2(type,name) DP_MTMP_1(type,name), type##2 name##2
#define DP_MTMP_3(type,name) DP_MTMP_2(type,name), type##3 name##3
#define DP_MTMP_4(type,name) DP_MTMP_3(type,name), type##4 name##4
#define DP_MTMP_5(type,name) DP_MTMP_4(type,name), type##5 name##5
#define DP_MTMP_6(type,name) DP_MTMP_5(type,name), type##6 name##6
#define DP_MTMP_7(type,name) DP_MTMP_6(type,name), type##7 name##7
#define DP_MTMP_8(type,name) DP_MTMP_7(type,name), type##8 name##8
#define DP_MTMP_9(type,name) DP_MTMP_8(type,name), type##9 name##9
#define DP_MTMP_10(type,name) DP_MTMP_9(type,name), type##10 name##10
// LP = LIST PARAM 列出参数
// SNMP = SINGLE NAME MULTI PARAM 单名字多参数
// 比如
// LP_SNMP_2(p); 就相当于
// p1, p2
#define LP_SNMP_0(pname)
#define LP_SNMP_1(pname) pname##1
#define LP_SNMP_2(pname) LP_SNMP_1(pname), pname##2
#define LP_SNMP_3(pname) LP_SNMP_2(pname), pname##3
#define LP_SNMP_4(pname) LP_SNMP_3(pname), pname##4
#define LP_SNMP_5(pname) LP_SNMP_4(pname), pname##5
#define LP_SNMP_6(pname) LP_SNMP_5(pname), pname##6
#define LP_SNMP_7(pname) LP_SNMP_6(pname), pname##7
#define LP_SNMP_8(pname) LP_SNMP_7(pname), pname##8
#define LP_SNMP_9(pname) LP_SNMP_8(pname), pname##9
#define LP_SNMP_10(pname) LP_SNMP_9(pname), pname##10
// LP = LIST PARAM 列出参数
// DNMP= DOUBLE NAME MULTI PARAM 双名字多参数
// 比如
// LP_DNMP_2(name, age); 就相当于
// name1, age1, name2, age2
#define LP_DNMP_1(pname1,pname2)
#define LP_DNMP_1(pname1,pname2) pname1##1, pname2##1
#define LP_DNMP_2(pname1,pname2) LP_DNMP_1(pname1,pname2), pname1##2, pname2##2
#define LP_DNMP_3(pname1,pname2) LP_DNMP_2(pname1,pname2), pname1##3, pname2##3
#define LP_DNMP_4(pname1,pname2) LP_DNMP_3(pname1,pname2), pname1##4, pname2##4
#define LP_DNMP_5(pname1,pname2) LP_DNMP_4(pname1,pname2), pname1##5, pname2##5
#define LP_DNMP_6(pname1,pname2) LP_DNMP_5(pname1,pname2), pname1##6, pname2##6
#define LP_DNMP_7(pname1,pname2) LP_DNMP_6(pname1,pname2), pname1##7, pname2##7
#define LP_DNMP_8(pname1,pname2) LP_DNMP_7(pname1,pname2), pname1##8, pname2##8
#define LP_DNMP_9(pname1,pname2) LP_DNMP_8(pname1,pname2), pname1##9, pname2##9
#define LP_DNMP_10(pname1,pname2) LP_DNMP_9(pname1,pname2), pname1##10, pname2##10