title: Have I misused templates here?
body: <p>I was trying to program generically for a Mesh class.  (I need CPU transformations to deform the meshes, which is why this <em>isn't</em> to be done in the GPU, in case you were thinking of asking)</p>
  
  <p>A <code>Mesh&lt;T&gt;</code> contains the vertices of a 3d shape, as well as the face tie-up (what vertices join together to make triangles).</p>
  
  <p>Here <code>T</code> is the type of vertex, some types of models have PNCT type (position, normal, color, texcoord) and others are just PC (position, color).</p>
  
  <pre><code>struct VertexPNCT {
      Vector3 pos, normal ;
      Vector4 color ;
      Vector2 tex ;
  } ;
  
  struct VertexPC {
      Vector3 pos ;
      Vector4 color ;
  } ;
  </code></pre>
  
  <p>of course, <strong>all</strong> vertices have a position!</p>
  
  <p>But here's my problem.</p>
  
  <p><code>Mesh&lt;T&gt;</code> has to implement a <code>transform</code> method.  Of course, every vertex format has a position, and if it's named consistently (<code>.pos</code>), then the method works fine:</p>
  
  <p>Since every vertex will definitely have a position, if I always call that member <code>.pos</code> in the <code>Vertex*</code> struct, then I can add a method <code>.transform</code> to the templated class <code>Mesh&lt;T&gt;</code> as:</p>
  
  <pre><code>void transform( Matrix4&amp; mat )
  {
      each vertex
        vertex.pos = mat * vertex.pos ; // transform it
  }
  </code></pre>
  
  <p>Now here's the screw up.  <em>If</em> the vertex format has a normal in it, then that normal has to be transformed as well (by the "normal matrix").  Now I have an "if statement" in my template?  I have to write a template specialization <code>.transform</code> method for each of the vertex types (which basically breaks into 2 categories, those with normals, and those without normals)?</p>
  
  <p>Have I misused templates here?  Missed some "programming boat"?</p>
  
  <p>Here's what I really want to do logically:</p>
  
  <pre><code>void transform( Matrix4&amp; mat )
  {
      each vertex in vertices
      {
        vertex.pos = mat * vertex.pos ; // transform it
        #ifdef vertex.normal
        vertex.normal = mat * vertex.normal ; // transform normal as well*
        #endif
      }
  }
  </code></pre>
  
  <p><sub>* Assumes no scale in transformation
  (then you don't need to use a "normal matrix")</sub></p>