任何人都可以使用示例代码来解释X-宏的用法吗? [英] Can any one explain about X-macros with example code to use them?

查看:55
本文介绍了任何人都可以使用示例代码来解释X-宏的用法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图详细了解X宏主题.但这并没有完全清楚.最好由任何一位专家用如何使用,如何调用"的示例来解释该主题.

I am trying to understand the X-macros topic in detail. But didn't get the full clarity on this. It would be better if any one of the expert will explain this topic with some example "how to use, how to call."

我找到了几篇文章,但是对此并没有完全清楚.在所有地方,他们都使用了我缺乏使用这些X宏的代码段.

I have found several articles, but didn't get the full clarity on this. In all places, they have used pieces of code where i am lacking in using of those X-macros.

提前致谢帕塔

推荐答案

您基本上 #define 变量列表作为占位符宏 X 的参数:

You basically #define a list of variables as parameters to an place holder macro X:

#define X_LIST_OF_VARS \
    X(my_first_var) \
    X(another_variable) \
    X(and_another_one)

然后使用模板:

#define X(var) do something with var ...
X_LIST_OF_VARS
#undefine X

制作代码块.例如,打印所有变量:

to make code blocks. For example to print all your vars:

#define X(var) printf("%d\n", var);
X_LIST_OF_VARS
#undefine X

将生成:

printf("%d\n", my_first_var);
printf("%d\n", another_variable);
printf("%d\n", and_another_one);

这篇关于任何人都可以使用示例代码来解释X-宏的用法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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