I have an enum class, say
enum class color { Red, Green, Blue, /* etc. */};
and I have class template,
template <color Color> class foo;
Now, I want to declare a data type which holds, for some (or all) possible values of color
, a value of type foo<color>
; and I want to be able to insert and remove such mappings at run-time.
So, I want something like an unordered_map from color to foo, except that for a given color, the mapping is not actually to any foo
, only to foo<color>
.
The closest thing I can think of using, that's also terse, is a
using MyMap = std::tuple<color::Blue, color::Red, color::Green>
(or perhaps std::optional
's of each of these foo's), so that given the color, I can use std::get<foo<MyColor>>(my_map));
and I can also write a getter which takes an actual color at compile-time:
template <color Color>foo<Color> get(const MyMap& map){ return std::get<foo<Color>>(map); }
but all that is a bit unwieldy. Can I do better?
Notes:
- In case there is nothing mapped from some color value - I don't mind the exact behavior: It could be an exception, returning a nullopt, or even failing to compile if your structure can only be filled at compile-time.
- You may use newer C++ language versions if you like (even C++26 if that helps you), but the less outlandish we need to be, the better.