如何生成类似于使用 C 宏的代码? #为了?

发布于 2025-01-15 20:15:53 字数 369 浏览 1 评论 0原文

是否有像 for 循环这样的 C 宏,可以生成代码供我使用函数?

例如:

SetSystem1();
SetSystem2();
SetSystem3();
...
SetSystem100();

可以编写

#for index = 0 to 100
    SetSystem##index();
#endfor

这样系统将生成 SetSystem1SetSystem100 吗?

有没有一些宏可以创建这样的代码?

我无法使用指向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

°如果伤别离去 2025-01-22 20:15:53

是否有一些宏可以创建这样的代码?

不,不是真的。即使您想创建这样的宏,您也必须阐明所有可能性 - 100 个案例需要 100 个宏。

您可以使用此类 maros 附带的库,例如 p99 https://gustedt.gitlabpages.inria.fr/p99/p99-html/group__preprocessor__for_gae3eb0e3a5216300874ed6cb9abc170ce.html#gae3eb0e3a5216300874ed6cb9abc170ce

#define P00_SEP(NAME, I, REC, RES) REC; RES
#define P00_VASSIGN(NAME, X, I)  SetSystem##I()
P99_FOR(A, 101, P00_SEP, P00_VASSIGN, , );

boost https://www.boost.org/doc/libs/ 1_78_0/libs/preprocessor/doc/index.html

#define DECL(z, I, text) SetSystem##I();
BOOST_PP_REPEAT_FROM_TO(1, 100, DECL, )

除了 C 预处理器之外,您还可以使用一些外部预处理器。例如使用 m4:

# forloop.m4, from documentation
divert(`-1')
# forloop(var, from, to, stmt)
define(`forloop',
  `pushdef(`$1', `$2')_forloop(`$1', `$2', `$3', `$4')popdef(`$1')')
define(`_forloop',
  `$4`'ifelse($1, `$3', ,
    `define(`$1', incr($1))_forloop(`$1', `$2', `$3', `$4')')')
divert`'dnl

forloop(`i', `1', `100', `dnl
`      SetSystem'i`();
'')

或使用 php:

<?php for ($i = 1; $i <= 100; $i++) echo "SystemOut".$i."();\n"; ?>

或使用 Python Jinja2:

{% for i in range(1, 100) %}
      SystemOut{{ i }}();
{% endfor %}

Is there are some macro can create code like this?

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 :

#define P00_SEP(NAME, I, REC, RES) REC; RES
#define P00_VASSIGN(NAME, X, I)  SetSystem##I()
P99_FOR(A, 101, P00_SEP, P00_VASSIGN, , );

Or boost https://www.boost.org/doc/libs/1_78_0/libs/preprocessor/doc/index.html :

#define DECL(z, I, text) SetSystem##I();
BOOST_PP_REPEAT_FROM_TO(1, 100, DECL, )

You can also use some external preprocessor additionally to C preprocessor. For example with m4:

# forloop.m4, from documentation
divert(`-1')
# forloop(var, from, to, stmt)
define(`forloop',
  `pushdef(`$1', `$2')_forloop(`$1', `$2', `$3', `$4')popdef(`$1')')
define(`_forloop',
  `$4`'ifelse($1, `$3', ,
    `define(`$1', incr($1))_forloop(`$1', `$2', `$3', `$4')')')
divert`'dnl

forloop(`i', `1', `100', `dnl
`      SetSystem'i`();
'')

or with php:

<?php for ($i = 1; $i <= 100; $i++) echo "SystemOut".$i."();\n"; ?>

or with Python Jinja2:

{% for i in range(1, 100) %}
      SystemOut{{ i }}();
{% endfor %}
还不是爱你 2025-01-22 20:15:53

C 预处理器中没有这样的功能。鉴于该工具的复杂性及其缺点,它不可能在未来版本的 C 标准中得到扩展。

您可以使用其他工具生成文本并将其包含在源文件中。以下是使用 bash shell 的示例:

for i in {1..100} ; do echo "    SetSystem$i();" ; done

或者,使用 tr 实用程序:

echo "SetSystem"{1..100}"();" | tr ' ' '\n'

或者如 Eric Postpischil 所评论,使用 jot< /code> 在 BSD 系统上:

jot -w '    SetSystem%d();' 100

但是 KamilCuk 找到了一个更简单、最优雅的解决方案:

printf "    SetSystem%d();\n" {1..100}

高级程序员的编辑器拥有强大的宏系统,使您能够交互式地生成相同的输出。

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:

for i in {1..100} ; do echo "    SetSystem$i();" ; done

Alternately, using the tr utility:

echo "SetSystem"{1..100}"();" | tr ' ' '\n'

Or as commented by Eric Postpischil, using jot on BSD systems:

jot -w '    SetSystem%d();' 100

But KamilCuk found a simpler and most elegant solution:

printf "    SetSystem%d();\n" {1..100}

Advanced programmers' editors have powerful macro systems that will enable you to produce the same output interactively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文