简体   繁体   English

C ++命令行宏预处理器无法替换单词

[英]c++ command line macros preprocessor fails to replace word

I am working in a Linux terminal-command line environment, no IDE. 我在Linux终端命令行环境中工作,没有IDE。 I compile my c++ programs with g++. 我用g ++编译我的c ++程序。 This one depends on using command line macros to execute different code statements without changing the source code itself. 这取决于使用命令行宏来执行不同的代码语句,而无需更改源代码本身。 Here is the chunk of code where I have an issue. 这是我遇到问题的代码块。 I have several different arrays that I want to perform a sorting on. 我要对几个不同的数组进行排序。 Then I have functions somewhere else in the source code that perform that sorting and return the sorted array. 然后,我在源代码中的其他地方具有执行该排序并返回已排序数组的函数。 I want to use command line macros to tell the preprocessor which array I want to use, and which sorting algorithm to use (which function to call). 我想使用命令行宏来告诉预处理器我要使用哪个数组,以及要使用哪种排序算法(要调用的函数)。 SORT_ALG should be replaced with the name of the function, and ARRAY should be replaced with the name of the array. SORT_ALG应该替换为函数的名称,而ARRAY应该替换为数组的名称。 So after preprocessing, the line should look like this: 因此,在预处理之后,该行应如下所示:

int* sorted_array = BubbleSort(array1, array1_size);

Here is the source code: 这是源代码:

  int array1[] = {24, 13, 9, 64, 7, 23, 34, 47};
  int array1_size = sizeof(array1) / sizeof(array1[0]);

  // an already sorted array!
  int array2[] = {1, 2, 5, 8, 10, 15, 20, 25, 30};
  int array2_size = sizeof(array2) / sizeof(array2[0]);

  // a reverse sorted array!
  int array3[] = {75, 50, 45, 30, 25, 18, 17, 12, 10, 6, 5}; 
  int array3_size = sizeof(array3) / sizeof(array3[0]);

  /*  
   * This code uses command line macros defined by g++
   * SORT_ALG should be one of the sorting function names such as:
   *   BubbleSort
   *   BubbleSortOptimized
   * ARRAY should be the name of one of the arrays, without the brackets:
   *   array1
   *   array2
   *   array3
   * Example of compiling the program with g++ using command line macros:
   *   g++ -D SORT_ALG=BubbleSort -D ARRAY=array1 sorting.cpp
   */
  int* sorted_array = SORT_ALG(ARRAY, ARRAY_size);
  cout << "The sorted array: ";
  PrintArray(sorted_array, ARRAY_size);
  cout << endl;

When I try to compile the source code, the preprocessor doesn't recognize to replace ARRAY_size with the corresponding variable: array1_size . 当我尝试编译源代码时,预处理器无法识别将ARRAY_size替换为相应的变量: array1_size

 $ g++ -D SORT_ALG=BubbleSort -D ARRAY=array1 sorting.cpp
sorting.cpp: In function ‘int main()’:
sorting.cpp:139:39: error: ‘ARRAY_size’ was not declared in this scope
   int* sorted_array = SORT_ALG(ARRAY, ARRAY_size);

I think that the preprocessor should recognize the ARRAY being array1 and then replace ARRAY_size with array1_size . 我认为预处理器应识别ARRAYarray1 ,然后将ARRAY_size替换为array1_size I think that it would be good to not have to define another command line macro to specify the size of the array because I would have to count the number of elements, and I'm going to be using this for situations where I don't know the size of the array in advance. 我认为最好不必定义另一个命令行宏来指定数组的大小,因为我必须计算元素的数量,并且在不使用这种情况的情况下,我将使用它提前知道数组的大小。 So I have the compiler determine the size of the array. 因此,我让编译器确定数组的大小。 Is the underscore the reason why the preprocessor fails its job? 下划线是预处理器无法完成其工作的原因吗? It is better to use a different naming convention for the size of the array, to make it be preprocessed correctly? 最好对数组的大小使用其他命名约定,以便对其进行正确的预处理? What other kind of approach would you suggest to fix this problem? 您会建议采用哪种其他方法来解决此问题?

From a preprocessor perspective, you can't define a macro FOO to be BAR and expect FOO_size to become BAR_size because FOO and FOO_size are different tokens. 从预处理程序的角度来看,您不能将宏FOO定义为BAR并且不能期望FOO_size成为BAR_size因为FOOFOO_size是不同的标记。

You can, however, create a paste macro to stage this: 但是,您可以创建一个粘贴宏来暂存此宏:

#define GLUE(a,b) GLUEI(a,b)
#define GLUEI(a,b) a##b
...
int* sorted_array = SORT_ALG(ARRAY, GLUE(ARRAY,_size));

The macro needs to come in a pair with an indirect macro to allow its arguments to expand. 宏需要与间接宏配对使用,以允许其参数扩展。 Due to macro expansion rules, ARRAY in the macro will expand first (given the indirection), which means if you define it to be something else that will apply before the pasting. 由于宏扩展规则,宏中的ARRAY将首先扩展(基于间接寻址),这意味着如果您将其定义为在粘贴之前将要应用的其他内容。

But outside the preprocessor perspective, why are you bothering to use the matching pair tokens anyway? 但是从预处理器的角度来看,为什么您仍然要使用匹配对标记呢? If array1_size is just going to be assigned to sizeof(array1) / sizeof(array1[0]) , you could just change this to SORT_ALG(ARRAY, (sizeof(ARRAY)/sizeof(ARRAY[0]))) . 如果将array1_size分配给sizeof(array1) / sizeof(array1[0]) ,则可以将其更改为SORT_ALG(ARRAY, (sizeof(ARRAY)/sizeof(ARRAY[0])))

(Also, what exactly are you doing here? It looks like there could be a much better implementation in C++ if you're doing anything more complex than benchmarking something specific; and I'm puzzled why you're using the command line to switch between algorithms). (此外,您在这里到底在做什么?如果您要执行比基准测试更复杂的事情,那么似乎可以在C ++中实现更好的实现;而且我很困惑您为什么使用命令行进行切换算法之间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM