title: C++ automatic generation of switch statement
body: <p>Consider the following code </p>
  
  <pre><code>#include &lt;iostream&gt;
  
  enum MyEnum{
      A,    
      B,
      END
  };
  
  template &lt;int N&gt;
  class Trait {};
  
  template&lt;&gt;
  class Trait&lt;A&gt; {
      public:
          static int funct(int i) {return i*3;}
  };
  
  template&lt;&gt;
  class Trait&lt;B&gt; {
      public:
          static int funct(int i) {return i*24;}
  };
  
  
  using namespace std;
  
  int main(){
      int i = 1;
      switch(i){
          case A: cout &lt;&lt; Trait&lt;A&gt;::funct(i) &lt;&lt; endl; break;
          case B: cout &lt;&lt; Trait&lt;B&gt;::funct(i) &lt;&lt; endl; break;
      }   
  } 
  </code></pre>
  
  <p>Which will print 24 on the screen.</p>
  
  <p>Assume now that I have many more values in the enum and that I define all the corresponding
  template specializations of the class Trait.</p>
  
  <p>To avoid writing all the code necessary in the switch statement I wrote a REPEAT macro which works almost like I want:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #define REPEAT(N, macro) REPEAT_##N(macro)
  #define REPEAT_0(macro)
  #define REPEAT_1(macro) REPEAT_0(macro) macro(0)
  #define REPEAT_2(macro) REPEAT_1(macro) macro(1)
  #define REPEAT_3(macro) REPEAT_2(macro) macro(2)
  #define REPEAT_4(macro) REPEAT_3(macro) macro(3)
  // etc...
  
  // enum and class definitions
  
  int main(){
     #define MY_MACRO(N) case N: cout &lt;&lt; Trait&lt;N&gt;::funct(i) &lt;&lt; endl; break;
  
     switch(i){
        REPEAT(2, MY_MACRO)
     }
  }
  </code></pre>
  
  <p>The problem I have with this approach is that I cannot use</p>
  
  <pre><code>REPEAT(END, MY_MACRO)
  </code></pre>
  
  <p>because the preprocessor doesn't know about my enum.</p>
  
  <p><strong>Question:</strong> Is there a way to generate automatically the switch statement?</p>
  
  <p><strong>Notes:</strong></p>
  
  <ul>
  <li>The situation where I have to use this is much more complicated and having something automated would be really helpful.</li>
  <li>It is important for me to use a switch statement because of the speed which can be achieved (speed is crucial for my application).</li>
  </ul>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p><strong>More notes:</strong></p>
  
  <ul>
  <li>It is important that the generation of the switch depends on the value of END defined in the enum.</li>
  </ul>
  
  <p><strong>EDIT 2/3</strong></p>
  
  <p>I decided to make an addition here to explain better my application and why I prefer some solutions to others</p>
  
  <ul>
  <li>In my real application the enum contains almost 50 different values and it will be extended in the future (hopefully by other people). The enum contains consecutive values.</li>
  <li>the class "Trait" has more than 1 member function (currently 5). Furthermore, I need to use all this in 5 different files. If I don't use an automated way of generating what I need I end up writing many times code which is basically the same.</li>
  <li>the member functions of Trait are used in the same way all the times.</li>
  <li><p>currently, inside my switch I have a function call which looks like this (in1, in2 and out are all double passed by reference, const for the first two cases).</p>
  
  <p>case A: Trait::funct(in1, in2, out); break;</p></li>
  </ul>
  
  <p>Why do I like templates?</p>
  
  <p>Consider the case Trait has 2 functions funct1 and funct2. I could define</p>
  
  <pre><code>template &lt;int N&gt;
  class Trait {
      public:
          static int funct1(int i){static_assert(N!=N, "You forgot to define funct1");}
          static int funct2(int i){static_assert(N!=N, "You forgot to define funct2");}
  };
  </code></pre>
  
  <p>Now, if a function definition is missing, the compiler will return a meaningful error. When other people will make additions this will be helpful.</p>
  
  <p>Using the method based on C++11 features suggested by Jarod42 I can avoid maintaining long arrays of function pointers which would be error prone.</p>
  
  <p><strong>Speed tests</strong></p>
  
  <p>So far I experimented with 3 solutions but with only two member functions in Trait:</p>
  
  <ul>
  <li>the solution suggested by Jarod42</li>
  <li>a simple array of function pointers as suggested by nndru and Ali</li>
  <li>switch statement with the RETURN macro</li>
  </ul>
  
  <p>The first two solutions seem to be equivalent, while the one based on the switch is 5 times faster. I used gcc version 4.6.3 with the flag -O3.</p>