如何生成C++定义调用的动态生成

How to generate Dynamic generation of C++ define calls

本文关键字:动态 调用 何生成 C++ 定义      更新时间:2023-10-16

我在 Cpp 代码中使用整数值进行了定义。我需要循环调用它们。我该怎么做?

// Defines:
#define A0 0
#define A1 1
#define A2 2
// ...
#define A50 50
// Now based on loop I need to call these defines
for (int i = 0; i <= 50; i++){
    function_name(A<i>, value);
}

#define宏仅在调用编译器之前由预处理器计算。因此,预处理器宏在运行时不存在,并且不能由使用仅在运行时知道值的变量构建的名称来引用。

对于您正在尝试的内容,您必须使用数组,例如:

#define A0 0
#define A1 1
#define A2 2
...
#define A50 50
const int A[51] = {A0, A1, A2, ..., A50};
...
for (int i = 0; i <= 50; i++){
    function_name(A[i], value);
}