迭代可变参数宏参数 [英] Iterating Variadic Macro Arguments

查看:95
本文介绍了迭代可变参数宏参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程生成一堆仿函数,以保持生成的code更具可读性我想拿出,将扩大线下一个宏,

I am programmatically generating bunch of functors, in order to keep the generated code more readable I am trying to come up with a macro that will expand line the following,

SET_STATE(FunctorA,a,b);

ref a;
ref b;
FunctorA(ref a, ref b){
   this->a = a;
   this->b = b;
}

基本上将扩大到第一个参数的构造函数。可变参数部分的参数构造函数的数量。是有可能的宏观内环路,并产生preprocessing在此code,即使它没有任何意义的这种特殊情况下,但我有一些函子有20个左右的变数,他们有它会清除访问我产生codeA不少。

Basically it will expand to the first arguments constructor. Variadic part is the number of arguments to the constructor. is it possible to loop inside the macro and generate this code during preprocessing even though it does not make sense for this particular case but I have some functors that have 20 or so variables that they have access to it will cleanup my generated code a lot.

所有参数将是相同的类型,则只有名称将有所不同。

All arguments will be of the same type, only names will differ.

推荐答案

使用欺骗在这个环节发现的 http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/ 计算参数的数量和使用一些真难看的宏我可以生成你想要的输出。

Using the tricked found in this link http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/ to count the number of arguments and using some really ugly macro I can generate the output you wanted.

我测试了它使用的 GCC (GCC -E TEST.CPP)和它的作品,这不是便携式

I tested it using gcc (gcc -E test.cpp) and it works, It's not portable.

code:

#define VA_NARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1)
#define SET_STATEGENERATE(name, count, ...)             \
        dec ## count (__VA_ARGS__)                      \
        name(ref ## count (__VA_ARGS__)) {              \
            con ## count (__VA_ARGS__)                  \
        }
#define SET_STATEP(name, count, ...) SET_STATEGENERATE(name, count, __VA_ARGS__) 
#define SET_STATE(name, ...) SET_STATEP(name, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
/* args */
#define dec1(a) ref a;
#define dec2(a,b) dec1(a) ref b;
#define dec3(a,b,c) dec2(a, b) ref c;
#define dec4(a,b,c,d) dec3(a,b,c) ref d;
#define dec5(a,b,c,d,e) dec4(a,b,c,d) ref e;
#define dec6(a,b,c,d,e,f) dec5(a,b,c,d,e) ref f;
#define dec7(a,b,c,d,e,f,g) dec6(a,b,c,d,e,f)ref g;
#define dec8(a,b,c,d,e,f,g,h) dec7(a,b,c,d,e,f,g) ref h;
#define ref1(a) ref a
#define ref2(a,b) ref1(a), ref b
#define ref3(a,b,c) ref2(a,b), ref c
#define ref4(a,b,c,d) ref3(a,b,c), ref d
#define ref5(a,b,c,d,e) ref4(a,b,c,d), ref e
#define ref6(a,b,c,d,e,f) ref5(a,b,c,d,e), ref f
#define ref7(a,b,c,d,e,f,g) ref6(a,b,c,d,e,f), ref g
#define ref8(a,b,c,d,e,f,g, h) ref7(a,b,c,d,e,f,g), ref h
#define con1(a) this->a = a;
#define con2(a,b) con1(a) this->b = b;
#define con3(a,b,c) con2(a,b) this->c = c;
#define con4(a,b,c,d) con3(a,b,c) this->d = d;
#define con5(a,b,c,d,e) con4(a,b,c,d) this->e = e;
#define con6(a,b,c,d,e,f) con5(a,b,c,d,e) this->f = f;
#define con7(a,b,c,d,e,f,g) con6(a,b,c,d,e,f) this->g = g;
#define con8(a,b,c,d,e,f,g,h) con7(a,b,c,d,e,f,g) this->h = h;

那么以下内容:

/* 2 args */
SET_STATE(FunctorAA, foo, bar)
/* 3 args */
SET_STATE(FunctorBB, foo, bar, baz)
/* 4 args */    
SET_STATE(FunctorCC, foo, bar, baz, qux)

会产生:

ref foo; ref bar; FunctorAA(ref foo, ref bar) { this->foo = foo; this->bar = bar; }
ref foo; ref bar; ref baz; FunctorBB(ref foo, ref bar, ref baz) { this->foo = foo; this->bar = bar; this->baz = baz; }
ref foo; ref bar; ref baz; ref qux; FunctorCC(ref foo, ref bar, ref baz, ref qux) { this->foo = foo; this->bar = bar; this->baz = baz; this->qux = qux; }

注意:的可以继续遵循明显的模式参数的个数

Note: you can continue the number of arguments following the obvious pattern.

这篇关于迭代可变参数宏参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆