c++ - 与 C++ 预处理器连接

标签 c++ concatenation c-preprocessor

我试图让预处理器根据已定义的名称生成一个列表。像这样

#define LIST "Start of list!"

#ifdef WITH_FOO
#define LIST LIST "Foo"
#endif

#ifdef WITH_BAR
#define LIST LIST "Bar"
#endif

#define LIST LIST "End of list!"

我从中得到一些关于 LIST 被重新定义的警告,然后在我尝试使用 LIST 的代码中它给出了一个错误:“LIST 未在此范围内声明”,除了警告,我相信它应该是。

我怎样才能让它工作?

最佳答案

你可以使用一些辅助宏:

#ifdef WITH_FOO
#  define LIST_FOO "Foo"
#else
#  define LIST_FOO
#endif

// ... ditto for BAR

#define FOO "Start of list!" LIST_FOO LIST_BAR "End of list!"

关于c++ - 与 C++ 预处理器连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9656266/

相关文章:

c# - 如何连接 ReadOnlyCollection<string> 和 IList<string>

javascript - 在 Javascript 中使用方括号来连接事物?

c++ - 使用预处理器从 C++ 中的字符串获取枚举

c++ - C/C++ : How should preprocessor directive work on macros argument list?

c++ - 在 C++ 文件 : Linking issues with Makefile 中使用 C extern 函数

c++ - 为什么 OpenCV 的 `cvCreateCameraCapture` 和 `cvCreateFileCapture` 不起作用?

c++ - 错误 : ("maybe you meant to use ' ->'?)

string - 如何打印 Groovy 列表并保留引号?

C语言 : how to interpret this #define?

c++ - cpp中的构造函数无限循环