初始化程序列表中的冗余宏用法 [英] Redundant macro usage in initializer lists
问题描述
使用可以成功减少代码重复之后我想进一步减少具有完全不同问题的代码重复
After I were able to successfully reduce code duplication with this I want to further reduce my code duplication with totally different problem
#include <iostream>
#include <array>
#define A1
#define B2
#define C3
#define D4
#define E5
enum Enum {
#ifdef A1
one,
#endif
#ifdef B2
two,
#endif
#ifdef C3
three,
#endif
#ifdef D4
four,
#endif
#ifdef E5
five,
#endif
end
};
const char* map(Enum e)
{
switch(e)
{
#ifdef A1
case one : return "one";
#endif
#ifdef B2
case two: return "two";
#endif
#ifdef C3
case three : return "three";
#endif
#ifdef D4
case four: return "four";
#endif
#ifdef E5
case five: return "five";
#endif
case end: return "end";
}
}
int main()
{
std::array<const char*, Enum::end + 1> arr{{
#ifdef A1
"a",
#endif
#ifdef B2
"b",
#endif
#ifdef C3
"c",
#endif
#ifdef D4
"d",
#endif
#ifdef E5
"e",
#endif
"z"
}};
int e = 0;
for (const auto& str : arr)
{
std::cout << str << " map " << map(Enum(e)) << std::endl;
e++;
}
return 0;
}
请注意函数 map
在我的代码中实际上并不存在,并且仅在此示例中实现。在上面的代码中,我有两个数组,它们的值用初始化列表初始化。我要解决的问题是需要两次 #ifdef
和 #endif
。相反,我想拥有一些类似的东西
Please note the function map
doesn't really exists in my code and was implemented only for this example. In the code above I have two array and their values initialized with initialization list. The problem I want to address is the need to have #ifdef
and #endif
twice. Instead, I want to have some thing like
#ifdef A1
#define A1_PAIR one, "a",
#else
#define A1_PAIR
#endif
#ifdef B2
#define B2_PAIR two, "b",
#else
#define B2_PAIR
#endif
#ifdef C3
#define C3_PAIR three, "c",
#else
#define C3_PAIR
#endif
#ifdef D4
#define D4_PAIR four, "d",
#else
#define D4_PAIR
#endif
#ifdef E5
#define E5_PAIR five, "e",
#else
#define E5_PAIR
#endif
#define MACRO(index) \
A1_PAIR \
B2_PAIR \
C3_PAIR \
D4_PAIR \
E5_PAIR \
end, "z"
enum Enum{ MACRO(1) };
std::array<const char*, Enum::end 1> arr2{{ MACRO(2) }};
显然,此代码不起作用,但我可以本着这种精神写些东西吗?
Obviously, this code doesn't work but can I write something in this spirit?
推荐答案
创建一个数组:
using arr_type=char const[];
#define ARRAY_SOURCE = arr_type{ "1", "a", "2", "b", /* etc */ }
使用 #ifdef
定义索引。
// elements.inc:
#ifdef A
ARRAY_SOURCE[0+OFFSET],
#endif
// etc
然后
std::array<const char*, NUM + 1> arr1{{
#define OFFSET 0
#include "elements.inc"
#undef OFFSET
}};
std::array<const char*, NUM + 1> arr2{{
#define OFFSET 1
#include "elements.inc"
#undef OFFSET
}};
这篇关于初始化程序列表中的冗余宏用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!