title: C++ template instantiation: Avoiding long switches
body: <p>I have a class depending on an integer template parameter.  At one point in my program I want to use one instantiation of this template, depending on a value of this parameter determined at runtime.  Here is a simple example demonstrating how I would go about this currently, using a big switch statement:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;type_traits&gt;
  
  template&lt;unsigned A&gt;
  struct Wrapper {
      typedef typename std::conditional&lt;A==1, int, float&gt;::type DataType;
      DataType content[A];
      void foo() {
          std::cout &lt;&lt; A &lt;&lt; std::endl;
      };
  };    
  
  int main(int argc, char *argv[])
  {
      std::string arg = argv[1];
      int arg_int = std::stoi(arg);
  
      switch (arg_int) {
      case 1: {
          Wrapper&lt;1&gt; w;
          w.foo();
          break;
      }
      case 2: {
          Wrapper&lt;2&gt; w;
          w.foo();
          break;
      }
      case 3: {
          Wrapper&lt;3&gt; w;
          w.foo();
          break;
      }
      default:
          return 1;
      };
  
      return 0;
  }
  </code></pre>
  
  <p>This will quickly get unwieldy once I have not only one parameter <code>A</code>, but multiple template arguments in various combinations.  Let's also assume that in reality there is a really good reason to implement A as a template parameter.</p>
  
  <p>Is there a way to replace the huge switch statement with almost identical case statements, e.g. using some metaprogramming magic from Boost or a preprocessor hack?</p>
  
  <p>Ideally I would like to be able write something like the following:</p>
  
  <pre><code>INSTANTIATE_DEPENDING(i, {1, 2, 3},
              {
                  Wrapper&lt;i&gt; w;
                  w.foo();
              }
      );
  </code></pre>