从 C++ 创建字符串列表和枚举列表;宏
为了使我的代码更短且更容易更改,我想
enum{ E_AAA, E_BBB, E_CCC };
static const char *strings{"AAA", "BBB", "CCC" };
用宏替换类似的内容,例如 INIT(AAA, BBB, CCC);但是当我尝试使用可变参数执行宏并进行字符串化时,我收到错误,因为未声明参数。
关于如何做到这一点有什么想法吗?
In order to make my code shorter and easier to change I want to replace something like
enum{ E_AAA, E_BBB, E_CCC };
static const char *strings{"AAA", "BBB", "CCC" };
With a macro, like INIT(AAA, BBB, CCC); but when I try doing a macro with variable arguments, and stringification I get an error as the arguments are not declared.
Any idea on how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是我几天前学到的解决方案。解决您问题的简化版本是:
但是您可以有一个改进的版本,带有函数调用,如下所示:
这将变成:
您甚至可以使用第一个元素的偏移,就像这个:
我希望这有帮助。
小心,
Beco
参考:
打印月份问题,作者:Kush,回答者:丹尼·瓦罗德
Here a solution I learned a few days ago. The simplified version that attends your question is:
But you can have an improved version, with a function call, like this:
This will grow to be:
You can even use an offset for the first element, like this one:
I hope this helps.
Take care,
Beco
Reference:
Print the month question, by Kush, answer by Danny Varod
您可以使用一些宏魔法来做到这一点:
这是一个测试程序:
它输出:
You can do it with a bit of macro magic:
Here is a test program:
It outputs:
这是我的解决方案:
诀窍是“fruit”是宏“FRUITS”的参数
并将被您传递给的内容所取代。
例如:
将扩展为:
让我们创建枚举和字符串数组:
Here is my solution:
The trick is that 'fruit' is an argument of the macro 'FRUITS'
and will be replaced by what ever you pass to.
For example:
will expand to this:
Lets create the enum and the string array:
下面是使用 Boost.Preprocessor 的解决方案:
(AAA)(BBB)(CCC)
是树元素 AAA、BBB 和 CCC 的 Boost.Preprocessor 序列;该宏将枚举名称附加到其模式中:Here is a solution with Boost.Preprocessor:
(AAA)(BBB)(CCC)
is a Boost.Preprocessor sequence of tree elements AAA, BBB and CCC; the macro append the enum name to it's modalities:一种方法是使用 X-Macros,它基本上是定义宏的方法,然后用于生成比简单宏容易允许的更复杂的结构。 这是一个完全按照您的要求进行操作的示例。
One way to do this is with X-Macros, which are basically a way to define a macro which is then used for generating more complex structures than a simple macro easily allows. Here is an example of doing exactly what you are asking.
我参加聚会有点晚了,但这里有另一个建议。
它创建一个强类型枚举类,例如
MyEnumName
和一个配套的静态帮助器类Enumator
。它比以前的答案更大,因为它具有更多功能,例如用于从/到字符串转换的流运算符。
请注意,由于使用索引序列,它依赖于 c++ 14 标准。
用途:
来源:
I am a bit late to the party but here is another suggestion.
It creates a strongly typed enum class, say
MyEnumName
and a companion static helper classEnumator<MyEnumName>
.It's bigger than previous answers as it has more features, e.g. stream operators for conversion from/to string.
Note that it relies on c++ 14 standard due to the use of index sequence.
Usage:
Source:
处理这个问题的一种方法是定义一个列表宏,即扩展为另一个留给用户定义的宏。例如:
定义
enum
:定义字符串: 就
我个人而言,我发现这比
X
宏更容易使用,因为它不依赖于 include-文件魔法。One way to handle this is to define a list macro, i.e. something that expands to another macro that is left for the user to define. For example:
To define the
enum
:To define the string:
Personally, I find this much easier to work with than the
X
macro, as this does not rely in include-file magic.对于一个简单的解决方案,我建议类似 X 宏。
对于添加其他几个功能(如范围检查、增强类型安全、可选关联数据等)的更复杂的解决方案,有一个建议(但从未最终确定)Boost.Enum 库。
For a simple solution, I'd recommend something like X-Macros.
For a more complex solution that adds several other features (like range checking, enhanced type safety, optional associated data, etc.), there's a proposed (but never finalized) Boost.Enum library.