title: C++ function switch on value
body: <p>I have a templated function</p>
  
  <pre><code>template &lt;typename S, typename T&gt;
  string DoStuff(const S&amp; s, const T&amp; t, const string&amp; format) {
      ...
  }
  </code></pre>
  
  <p>But <code>format</code> changes the function's behaviour completely (imagine values like "csv", "json", etc.).  So it would make a bit more sense to have separate functions.</p>
  
  <p>I feel like I'm missing something obvious, that this should be possible with some sort of specialisation.  Otherwise, I just have to add a dispatch function, which would work but feels like I've missed something obvious.</p>
  
  <pre><code>template &lt;typename S, typename T&gt;
  string DoStuff(const S&amp; s, const T&amp; t, const string&amp; format) {
      if (format == "csv")
          return DoStuffCSV(s, t);
      if (format == "json")
          return DoStuffJSON(s, t);
      // ...
  }
  
  template &lt;typename S, typename T&gt;
  string DoStuffCSV(const S&amp; s, const T&amp; t) {
  ...
  }
  
  template &lt;typename S, typename T&gt;
  string DoStuffJSON(const S&amp; s, const T&amp; t) {
  ...
  }
  </code></pre>
  
  <p>Conceptually, I want this (which of course doesn't work)</p>
  
  <pre><code>template &lt;typename S, typename T, string("csv")&gt;
  string DoStuff(const S&amp; s, const T&amp; t) {
  ...
  }
  
  template &lt;typename S, typename T, string("json")&gt;
  string DoStuff(const S&amp; s, const T&amp; t) {
  ...
  }
  </code></pre>