title: Can I define a static map as a function argument in the actual function call?
body: <p><strong>UPDATE:</strong> I just did some benchmarking of the core conversion logic for various solutions. Here are the CPU times in <em>nanoseconds</em> for a single iteration, averaged out of five million iterations on <a href="http://melpon.org/wandbox/" rel="nofollow">Wandbox</a>:</p>
  
  <p>8 - switch statement</p>
  
  <p>176 - static map</p>
  
  <p>3800 - dynamic map</p>
  
  <p>As you can see, creating a new map every time (dynamic mapping) is relatively time consuming. Creating the map once (static mapping) and just referring to it thereafter is almost <strong>22 times faster</strong>. But, coincidentally, the original switch-based solution is <strong>22 times faster</strong> than static mapping!</p>
  
  <p>Hmm... I never considered dynamic mapping a viable solution but had hopes for static mapping. It's quite a bit slower than the good, old <code>switch</code> statement, but does 8 nanoseconds versus 176 nanoseconds really matter?</p>
  
  <p><strong>End of update</strong></p>
  
  <hr>
  
  <p>The <strong>core problem</strong> I'm trying to solve is translating from one "namespace" to another (in the general sense, not re the C++ <code>namespace</code> keyword). This is common in programming, usually solved in C++ with a <code>switch</code> statement like this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  enum MUSIC { ROCK, RAP, EDM, COUNTRY };
  enum COLOR { RED, BLUE, ORANGE, WHITE };
  
  COLOR convert(MUSIC music)
  {
      COLOR color = WHITE;
      switch (music) {
      case RAP: color = RED; break;
      case EDM: color = BLUE; break;
      case ROCK: color = RED; break;
      }
      return color;
  }
  
  int main()
  {
      COLOR c = convert(COUNTRY);
      std::cout &lt;&lt; c &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>The following is <strong>my solution</strong> (the templates will be tucked away in a header file somewhere). The <code>convert()</code> function works with any types that are valid for <code>std::map</code>, e.g., <code>enum</code> to <code>enum</code> and <code>long</code> to <code>std::string</code>. The first parameter is the key, or "from" value, the second is the default value if can't be converted, and the return value is the mapped, or "to," value. (Special thanks to @Yakk for help with template argument deduction.)</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include "boost/assign.hpp"
  
  template&lt;class T&gt; struct no_deduction { typedef T type; };
  template&lt;typename Key, typename T&gt;
  T convert(const Key &amp;k, const T &amp;d, const typename no_deduction&lt;std::map&lt;Key, T&gt; &gt;::type &amp;m) {
      typename std::map&lt;Key, T&gt;::const_iterator it = m.find(k);
      return it == m.end() ? d : it-&gt;second;
  }
  
  using boost::assign::map_list_of;
  
  enum MUSIC { ROCK, RAP, EDM, COUNTRY };
  enum COLOR { RED, BLUE, ORANGE, WHITE };
  
  int main()
  {
      COLOR c = convert(COUNTRY, WHITE, map_list_of (RAP, RED) (EDM, BLUE) (ROCK, RED));
      std::cout &lt;&lt; c &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Anyway, notice that <code>map_list_of</code>, above, creates the same list every time it's called. I'd like it to be <strong>static</strong>. This is the obvious solution:</p>
  
  <pre><code>    static const std::map&lt;MUSIC, COLOR&gt; m = map_list_of (RAP, RED) (EDM, BLUE) (ROCK, RED);
      COLOR c = convert(COUNTRY, WHITE, m);
  </code></pre>
  
  <p>But I'm trying to make it simple to use, in particular, as a single statement. Here's a <strong>macro</strong> solution, but I'd rather avoid that, too.</p>
  
  <pre><code>#define CONVERT(f,t,d,m) do{static const std::map&lt;BOOST_TYPEOF(f), BOOST_TYPEOF(d)&gt; m_ =\
      map_list_of m; t = convert(f, d, m_);}while(0)
  
      COLOR c;
      CONVERT(COUNTRY, c, WHITE, (RAP, RED) (EDM, BLUE) (ROCK, RED));
  </code></pre>
  
  <hr>
  
  <p>Does anybody know of some C++ <strong>magic</strong> (C++03, actually) that can call my <code>convert()</code> function</p>
  
  <ol>
  <li>with a static map,</li>
  <li>as a single statement, and</li>
  <li>without using a macro?</li>
  </ol>