The idiomatic way to do this at compile time in C ++ is using traits.
As an example:
enum Foo { ONE, TWO }; enum Bar { THREE, FOUR }; template<Foo> struct conv; template<> struct conv<Foo::ONE> { static constexpr Bar value = Bar::THREE; }; template<> struct conv<Foo::TWO> { static constexpr Bar value = Bar::FOUR; };
If you want to do this at run time, the switch might be a good fit.
Anyway, you can still use traits to centralize the transformation logic and do something like this:
Bar get(Foo choice) { switch(choice) { case Foo::ONE: return conv<ONE>::value; case Foo::TWO: return conv<TWO>::value; } }
source share