对于在预处理器阶段展开的循环宏? [英] For loop macro which unrolled on the pre-processor phase?
问题描述
我想用gcc预处理器编写几乎相同的代码声明500次。让我们说出于演示的目的,我想使用宏 FOR_MACRO
:
#define FOR_MACRO(x)\
#for {1 ... x}中的i:\
const int arr_len _ ## x [i] = {i};
并呼叫 FOR_MACRO(100)
转换为:
const int arr_len_1 [1] = {1};
const int arr_len_2 [2] = {2};
...
const int arr_len_100 [100] = {100};
这不是个好主意:
尽管原则上可行,但使用预处理器意味着您必须至少手动展开循环一次,最终会得到一些对循环深度的任意实现定义限制,并且会生成所有语句。
更好地使用您选择的脚本语言生成代码(可能位于单独的可包含文件中),并将其与您的构建过程集成在一起。
p>I want to use gcc pre-processor to write almost the same code declaration for 500 times. let's say for demonstration purposes I would like to use a macro FOR_MACRO
:
#define FOR_MACRO(x) \
#for i in {1 ... x}: \
const int arr_len_##x[i] = {i};
and calling FOR_MACRO(100)
will be converted into:
const int arr_len_1[1] = {1};
const int arr_len_2[2] = {2};
...
const int arr_len_100[100] = {100};
This is not a good idea:
While possible in principle, using the preprocessor means you have to manually unroll the loop at least once, you end up with some arbitrary implementation-defined limit on loop depth and all statements will be generated in a single line.
Better use the scripting language of your choice to generate the code (possibly in a separate includeable file) and integrate that with your build process.
这篇关于对于在预处理器阶段展开的循环宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!