title: C++ Switch on Data Type
body: <p>I have an <code>Attribute</code> class that has an Enum specifying the type of the attribute (<code>INT_64, UINT 64, INT_32, STRING, DOUBLE, etc.</code>). This Attribute class uses boost::any to hold a vector of the types specified by the enum.</p>
  
  <p>At the moment in order to work with this data I have a big switch statement, and at least for the fundamental data types I feel like there would be an easier way to do it.</p>
  
  <p>My switch statement looks something like this:</p>
  
  <pre><code>switch(attribute.type) {
      case DOUBLE:
          stmt-&gt;setNumber(col_counter, Number(attribute.get_value&lt;double&gt;(row_counter)));
          break;
      case INT_32:
          stmt-&gt;setNumber(col_counter, Number(attribute.get_value&lt;int_32t&gt;(row_counter)));
          break;
  }
  </code></pre>
  
  <p>Attribute is defined as:</p>
  
  <pre><code>class Attribute
  {
      public:
          template &lt;typename T&gt;
          T get_value(const unsigned index) const
          {
              const std::vector&lt;T&gt; * v = boost::any_cast&lt;const std::vector&lt;T&gt; &gt;(&amp;data);
              return v-&gt;at(index);
          }
  
          Data_Type_Enum type;
          std::string name;
          boost::any data;
  }
  </code></pre>
  
  <p>Is there a way of avoiding the switch statement, doing something similar to:</p>
  
  <pre><code>stmt-&gt;setNumber(col_counter, Number(attribute.get_value&lt;attribute.type&gt;(row_counter)));
  </code></pre>