问题
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;
}
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?
回答1:
Create one array:
using arr_type=char const[];
#define ARRAY_SOURCE = arr_type{ "1", "a", "2", "b", /* etc */ }
use #ifdef to define indexes.
// elements.inc:
#ifdef A
ARRAY_SOURCE[0+OFFSET],
#endif
// etc
then
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
}};
回答2:
You might use pair/tuple to group data together:
std::array<std::pair<const char*, const char*>, NUM + 1> arr{{
#ifdef A1
{"1", "a"},
#endif
#ifdef B2
{"2", "b"},
#endif
#ifdef C3
{"3", "c"},
#endif
#ifdef D4
{"4", "d"},
#endif
#ifdef E5
{"5", "e"},
#endif
{"26", "z"}
}};
for (const auto& p : arr) {
std::cout << p.first << std::endl;
}
for (const auto& str : arr) {
std::cout << p.second << std::endl;
}
std::cout << "ok" << std::endl;
来源:https://stackoverflow.com/questions/47752997/redundant-macro-usage-in-initializer-lists