Well, this argument is really pointless. A switch would work fine... I'm not denying that. Of course, a map may theoretically be less efficient, but there are a few things to consider:Originally Posted by Daved
a) Even if you look up the name occasionally (i.e. more than once), is it really going to be often enough to make a difference?
b) Are you really going to have enums that are so broad in their range of values that this matters?
c) As an enum is fixed at compile time, and cannot scale, the fact that searching a map scales as lg really means nothing.
d) There are other capabilities of maps that might make them more desirable (one of them being that they play nice with the rest of STL).
So, in conclusion to my contributions to this thread, if you want to stick a switch in a function that returns a string (or other identifier), that is fine (I never said otherwise). A std::map would also work quite handily. Don't use an array though, as you could very easily break it (perhaps it would only mean an offset, perhaps a function to map enum values to array indices -- it is still not easily maintainable code).
And JoshR is correct. Enums must be set to constant values.