title: C++ template method to specialize union types
body: <p>Suppose I have a union:</p>
  
  <pre><code>union U{
      int i;
      float f;
  };
  </code></pre>
  
  <p>I want to write a generic method that uses it as a float or int. Something like this:</p>
  
  <pre><code>template &lt;typename T&gt;
  T sum(std::vector&lt;U&gt; vec){
      T res(0);
      for (U &amp;v: vec){
          res += ... // use v.i or v.f depending on what T is
      }
      return res;
  }
  </code></pre>
  
  <p>Is there a way to do this? This was just an example method. I have a much longer, complicated method, and I don't want to duplicate it just to switch which union type it is using.</p>