简体   繁体   English

使 C++ 预处理器在每行末尾添加一个字符

[英]Make C++ preprocessor add a character at the end of each line

I am implementing some compile-time check which should base on a CSV file and I am trying to load it a bit hacky way:我正在实施一些编译时检查,该检查应基于 CSV 文件,并且我正在尝试以一种有点hacky的方式加载它:

constexpr std::variant<int, const char*> test[] = {
#include "data.csv"
};

where "data.csv" is:其中“data.csv”是:

  "a","b","c"
  1,"xyz",1
  2,"abc",0
  3,"def",1

Obviously, there is a problem with the lack of commas at the end of each line.显然,每行末尾缺少逗号是有问题的。 Is there a way to add a comma with C++ (or even Boost) preprocessor at the end of each line?有没有办法在每行末尾添加一个带有 C++(甚至是 Boost)预处理器的逗号?

I know I could add some token at the end of each line and then replace it with #define , but I would like to avoid it for the sake of clarity.我知道我可以在每行的末尾添加一些标记,然后用#define替换它,但为了清楚起见,我想避免它。 Can one replace a \n character with #define ?可以用#define替换\n字符吗?

Is there a way to add a comma with C++ (or even Boost) preprocessor at the end of each line?有没有办法在每行末尾添加一个带有 C++(甚至是 Boost)预处理器的逗号?

Yes, create a macro:是的,创建一个宏:

#define ADD_COMMA(...)  __VA_ARGS__ ,

Then change each line of the file to:然后将文件的每一行更改为:

  ADD_COMMA("a","b","c")
  ADD_COMMA(1,"xyz",1)
  ADD_COMMA(2,"abc",0)
  ADD_COMMA(3,"def",1)

If by "is there a way to add a comma" you mean reading the file line by line in preprocessor and calling some macro on each line, then no, that is not possible.如果“有没有办法添加逗号”是指在预处理器中逐行读取文件并在每一行上调用一些宏,那么不,这是不可能的。 Such functionality is broadly out-of-scope for a C preprocessor.此类功能在 C 预处理器的范围之外。

BTW your problem looks oddly trivial - just add a comma on the end of each line to the file with an external tool.顺便说一句,您的问题看起来很奇怪-只需使用外部工具在文件的每行末尾添加一个逗号即可。

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

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