I am writing a function that takes the enum MyEnum
as input. Depending on the value of MyEnum, I initialize an object and process it. I cannot change the class hierarchy of those objects (their classes), since they are from an external dependency. Processing the newly created object is always the same.
Question:
How can I write this "elegantly". I do not want to write a long if, else if, else block that repeats itself over and over, which is what I currently do. What I would like to have is some magic function that tells me the type to use at runtime. Afaik this does not exist.
What I would like to have:
magic_function(enum_known_at_runtime) my_object;
What I would use if I knew the value at compile time: play with code
enum class MyEnum { A, B , /* many more values */ };
template <MyEnum>
struct get_type;
template <>
struct get_type<MyEnum::A> {
using Type = unsigned int; // some type
};
...
int main() {
get_type<enum_known_at_compiletime>::Type i;
// do stuff with i
}
What I currently do:
if (enum_known_at_runtime == MyEnum::A) {
// init an object of some type and do stuff with it
} else if (enum_known_at_runtime == MyEnum::A) {
// init an object of other type and do same stuff with it
} ...
Let us assume you capture the repeated code in a templated function and define corresponding instantiations for
get_type
:You can now write your repeated code as follows:
Now the work for new enums is adding a single line and an extra case for
get_type
, which you can further shrink using a macro. As a bonus, the compiler will yell at you if yourswitch
does not cover all cases.