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?
Create one array:
use
#ifdef
to define indexes.then
You might use pair/tuple to group data together: