# Mapping enum to enum

I need to map from one enum type to another. Here is the enum I am given:

enum CfgFruitType { CFGNoFruit, CFGApple, CFGApricot, CFGBanana,
CFGMango, CFGKiwi, CFGFig, CFGGrape, CFGMelon, CFGLemon,
CFGNectarine, CFGPineapple, CFGWatermelon, CFGCherry, CFGPear,
CFGStrawberry };


and I need to map this to a second enum:

enum FAddressType { AddressTypeUnknown, AddressTypeCherry,


Here is my implementation:

FAddressType maptype(CfgFruitType cfgtype) {
int mapper[16][2] =
{
};

}


Is this the best approach? How can it be improved?

You made a look-up table. Here's 2 things to note:

1. Your look up table is between 2 types, but your table is only 1 type. You break type safety. They are enums and it's 'safe' if the enums have the same underlying type, but dirty either way.

2. A switch frequently generates a look up table for you, and in this case definitely will on any decent compiler. It will not generate an if-if else-else chain. See the following:

:

FAddressType maptype(CfgFruitType cfgtype)
{
switch(cfgtype)
{
default:            assert(!"Not a valid CfgFruitType!"); return AddressTypeUnknown;
}
}


Shorter code, very clear, easy to add new cases, very likely to outperform (by a negligible margin) your code.

Or, you could be less explicit and make it really short:

switch(cfgtype)
{