如何生成类似于使用 C 宏的代码? #为了?
是否有像 for
循环这样的 C 宏,可以生成代码供我使用函数?
例如:
我
SetSystem1();
SetSystem2();
SetSystem3();
...
SetSystem100();
可以编写
#for index = 0 to 100
SetSystem##index();
#endfor
这样系统将生成 SetSystem1
到 SetSystem100
吗?
有没有一些宏可以创建这样的代码?
我无法使用指向 C 库的数组指针。
Is there are C macro like the for
loop, that can generate code for me to use functions?
for example:
I have
SetSystem1();
SetSystem2();
SetSystem3();
...
SetSystem100();
can I write
#for index = 0 to 100
SetSystem##index();
#endfor
so the system will generate SetSystem1
to SetSystem100
?
Is there are some macro can create code like this?
I can't use array pointer to the C library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,不是真的。即使您想创建这样的宏,您也必须阐明所有可能性 - 100 个案例需要 100 个宏。
您可以使用此类 maros 附带的库,例如
p99
https://gustedt.gitlabpages.inria.fr/p99/p99-html/group__preprocessor__for_gae3eb0e3a5216300874ed6cb9abc170ce.html#gae3eb0e3a5216300874ed6cb9abc170ce :或
boost
https://www.boost.org/doc/libs/ 1_78_0/libs/preprocessor/doc/index.html:除了 C 预处理器之外,您还可以使用一些外部预处理器。例如使用
m4
:或使用 php:
或使用 Python Jinja2:
No, not really. Even if you want to create such macro, you have to spell out all possibilities - it will take 100 macros for 100 cases.
You can use libraries that come with such maros, like
p99
https://gustedt.gitlabpages.inria.fr/p99/p99-html/group__preprocessor__for_gae3eb0e3a5216300874ed6cb9abc170ce.html#gae3eb0e3a5216300874ed6cb9abc170ce :Or
boost
https://www.boost.org/doc/libs/1_78_0/libs/preprocessor/doc/index.html :You can also use some external preprocessor additionally to C preprocessor. For example with
m4
:or with php:
or with Python Jinja2:
C 预处理器中没有这样的功能。鉴于该工具的复杂性及其缺点,它不可能在未来版本的 C 标准中得到扩展。
您可以使用其他工具生成文本并将其包含在源文件中。以下是使用
bash
shell 的示例:或者,使用
tr
实用程序:或者如 Eric Postpischil 所评论,使用
jot< /code> 在 BSD 系统上:
但是 KamilCuk 找到了一个更简单、最优雅的解决方案:
高级程序员的编辑器拥有强大的宏系统,使您能够交互式地生成相同的输出。
There is no such facility in the C preprocessor. Given the complexity of this tool and its shortcomings, there is no chance that it be extended in future versions of the C Standard.
You can use another tool to generate the text and include that in your source file. Here is an example using the
bash
shell:Alternately, using the
tr
utility:Or as commented by Eric Postpischil, using
jot
on BSD systems:But KamilCuk found a simpler and most elegant solution:
Advanced programmers' editors have powerful macro systems that will enable you to produce the same output interactively.