Interesting. How about using a hash_map instead of a map though, might be a little bit faster for larger versions.
Normally I would just create my own jump table, or if it is something where the case statements are a single line of code (any complexity, just as long as it is a single line, like a function call or setting something) then it should be pretty easy to make up a few templated commands to do it, probably a mock-up of something like this:
// This would be the preferred syntax:
str_switch(testString)
[
str_case<"someLiteral", fallthrough>(commandToPerform_LambdaWouldBeFine),
str_case<"anotherLiteral", break>(anotherCommandToPerform_LambdaWouldBeFine),
str_default_case(theDefaultCommandToPerform_LambdaWouldBeFine)
]
But as the current C++ does not support character array template params then you could cast the pointer to an int and pass that in and just let it convert it back, hiding the nasties in a macro probably (D's templates supports string literals just fine, grrr, C++'s templates *really* need to be more like D's). Or maybe convert the above to the equivalent, but much more nasty:
str_switch(testString)
[
str_case<fallthrough, 's','o','m','e','L','i','t','e','r','a','l'>(commandToPerform_LambdaWouldBeFine),
str_case<break, 'a','n','o','t','h','e','r','L','i','t','e','r','a','l'>(anotherCommandToPerform_LambdaWouldBeFine),
str_default_case(theDefaultCommandToPerform_LambdaWouldBeFine)
]
Which would not be hard to do at all actually, but yea... great fun with that syntax, and I cannot off-hand think of any way to have the preprocessor (or the horrible string template support) to convert a nice string into a character template array like that...
The nice thing about the above two forms is that you could pre-process the hash of the strings before execution so that the testString could be parsed as normal and matched at run-time, with a test if the hash matches to confirm, else dump to the default.
The D language (I would quite honestly use if it had the rich amount of libraries that C++ has or some interaction with them, which it does, but only with C externaled code, no C++ class external support thanks to C++'s lack of standardization in its external support) supports string in switches, which it has highly optimized so it is just about as quick as a hash of the incoming string, the rest of the work is at compile time, enhanced
example from the D site:
char[] name;
/* code to set name to something */
switch (name)
{
case "hello":
// do something
// fallthrough
case "world":
// do something else
break;
case "fred":
// do something yet more
// then do the things in the default label
goto default;
case "sally":
// do yet even more somethings
// goto the case "world" as well
goto case "world";
default:
// do the default thing
}