title: C/C++: switch for non-integers
body: <p>Often I need to choose what to do according to the value of a non-POD constant element, something like this:</p>
  
  <pre><code>switch( str ) {
    case "foo": ...
    case "bar": ...
    default:    ...
  }
  </code></pre>
  
  <p>Sadly <code>switch</code> can only be used with integers: <code>error: switch quantity not an integer</code>.</p>
  
  <p>The most trivial way to implement such thing is then to have is a sequence of <code>if</code>s:</p>
  
  <pre><code>if( str == "foo" )      ...
  else if( str == "bar" ) ...
  else                    ...
  </code></pre>
  
  <p>But this solution looks dirty and should cost O(n), where n is the number of cases, while that piece of code could cost O(log n) at the worst case with a binary search.</p>
  
  <p>Using some data structs (like Maps) it could be possible to obtain an integer representing the string ( O(log n) ), and then use an O(1) <code>switch</code>, or one could implement a static binary sort by nesting <code>if</code>s in the right way, but still these <em>hacks</em> would require a lot of coding, making everything more complex and harder to maintain.</p>
  
  <p>What's the best way to do this? (fast, clean and simple, as the <code>switch</code> statement is)</p>