К сожалению, все, что вы пытаетесь сделать, невозможно. Директива (##) невозможно использовать в объявлении макроса. Его можно использовать только в определении как таковом.
#define glue(a,b) a ## b
glue(c,out) << "test";
Пример заимствован из cplusplus.com
Ниже я написал пример того, что вы хотите сделать.
#include <stdio.h>
#define prefix_1 1
#define prefix_2 2
#define prefix_3 3
#define macro_##prefix_1 "macro_1"
#define macro_##prefix_2 "macro_2"
#define macro_##prefix_3 "macro_3"
int main(){
printf("%s\n%s\n%s\n", macro_prefix_1, macro_prefix_2, macro_prefix_3);
return 0;
}
При попытке скомпилировать вышеуказанный код вы получите этот журнал построения.
||=== Build: Debug in file_test (compiler: GNU GCC Compiler) ===|
main.cpp|7|warning: missing whitespace after the macro name [enabled by default]|
main.cpp|7|error: '##' cannot appear at either end of a macro expansion|
main.cpp|8|warning: missing whitespace after the macro name [enabled by default]|
main.cpp|8|error: '##' cannot appear at either end of a macro expansion|
main.cpp|9|warning: missing whitespace after the macro name [enabled by default]|
main.cpp|9|error: '##' cannot appear at either end of a macro expansion|
main.cpp||In function 'int main()':|
main.cpp|13|error: 'macro_prefix_1' was not declared in this scope|
main.cpp|13|error: 'macro_prefix_2' was not declared in this scope|
main.cpp|13|error: 'macro_prefix_3' was not declared in this scope|
||=== Build failed: 6 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|
Так что если вы хотите иметь макросы, вам просто нужно будет добавить префикс нормально. К счастью, вы в основном уже делали это, но добавляли «##». Надеюсь, это помогло.
Кратко, нет. По крайней мере, не так. –
Вы пробовали? В gcc параметр -E даст вам результаты этапа препроцессора. – John3136
См. [Как объединить два раза с препроцессором C и развернуть макрос как в «' arg ## _ ## MACRO'??] (Http://stackoverflow.com/questions/1489932/how-to-concatenate-twice -with-the-c-preprocessor-and-expand-a-macro-as-in-arg/1489985 # 1489985) для подробного обсуждения того, как работает маркерная конкатенация. Вы не можете использовать '##' в имени макроса, как показано в вопросе; это должно быть в расширении функционально-подобного макроса. –