为什么序列迭代在C宏中起作用? [英] Why does sequence iteration work in C macro?

查看:90
本文介绍了为什么序列迭代在C宏中起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写C宏时,有一个技巧叫做序列迭代".看起来如下:

When writing C macro, there is a trick called "sequence iteration". It looks like as follow:

#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
#define FUNCTION(name) void name();
#define FUNCTION_TABLE(seq) CAT(FUNCTION_TABLE_1 seq, _END)
#define FUNCTION_TABLE_1(x) FUNCTION(x) FUNCTION_TABLE_2
#define FUNCTION_TABLE_2(x) FUNCTION(x) FUNCTION_TABLE_1
#define FUNCTION_TABLE_1_END
#define FUNCTION_TABLE_2_END

FUNCTION_TABLE((x) (y) (z) (e))

序列(即FUCTION_TABLE的参数)将被一一处理.但是,据我所知,令牌不会在相同范围内扩展两次.因为它是漆成蓝色".展开FUNCTION_TABLE_2时,宏FUNCTION_TABLE_1已被绘制.为什么它仍在扩展?

The sequence, i.e. the argument of FUCTION_TABLE, will be processed one by one. However, as far as I know, a token will not be expanded twice in the same scope. Because it is "painted blue". When FUNCTION_TABLE_2 is expanded, the macro FUNCTION_TABLE_1 has already been painted yet. Why is it still expanded?

推荐答案

这个想法是,在宏扩展内,所有参数扩展都将从相同的BLUE-SET开始.

The idea is that inside a macro expansion all the expansion of parameters will start with the same BLUE-SET.

,在 FUNCTION_TABLE_1 seq 内部的所有参数的扩展将始终以BLUE-SET = {seq}开始.因此,在进入FUNCTION_TABLE_1内部之后,在BLUE-SET中添加了 x ,但是完成此操作后,它又回到了 FUNCTION_TABLE 的范围,在此范围内,扩展再次以 x> BLUE-SET = {seq} .

inside FUNCTION_TABLE(seq) the expansion of ALL parameters inside FUNCTION_TABLE_1 seq will start all the time with BLUE-SET={seq}. So after entering inside the FUNCTION_TABLE_1 the BLUE-SET is added the x, but when this finishes it comes back to the scope of FUNCTION_TABLE where the expansion starts again with the BLUE-SET={seq}.

因此,第一次对其进行扩展 FUNCTION_TABLE_1(x)并在 BLUE-SET = {seq,x} 内进行此扩展,但是当> FUNCTION_TABLE_1 完成后,返回到 FUNCTION_TABLE ,然后将从该范围扩展 FUNCTION_TABLE_2(y)和INSIDE FUNCTION_TABLE_2 再次设置BLUE-SET = {seq,x},等等.

So, the first time it is expanded FUNCTION_TABLE_1(x) and INSIDE this expansion the BLUE-SET={seq,x} but when the expansion of FUNCTION_TABLE_1 finishes, it comes back to FUNCTION_TABLE and it will expand next FROM this scope the FUNCTION_TABLE_2(y) and INSIDE FUNCTION_TABLE_2 the BLUE-SET={seq, x} again, etc.

这篇关于为什么序列迭代在C宏中起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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