title: Generic Vector Class Implementation
body: <p>I want to implement numerical <code>Vector</code> which has several numerical operations.</p>
  
  <p>I used template to define generic version of vector.</p>
  
  <p>But implementation should be different with concrete implementation like <code>Vector3</code>.</p>
  
  <p>For example, <code>+</code> operation would look like these.</p>
  
  <p><strong>Vector3 implementation</strong></p>
  
  <pre><code>Vector3 operator+(const Vector3&amp; vec3) const{
      return Vector3(data[0]+vec3.data[0], data[1]+vec3.data[1], data[2]+vec3.data[2]);
  }
  </code></pre>
  
  <p><strong>template implementation</strong></p>
  
  <pre><code>GenericVector operator+(const GenericVector&lt;T,N&gt;&amp; vec) const{
      GenericVector temp = *this;
      for(int i=0;i&lt;N;i++)
      { 
          temp.data[i] += vec.data[i];
      }
      return temp;
  }
  </code></pre>
  
  <p>Now that we cannot know how many <code>data</code> it would have, we cannot define constructor like <code>Vector3(x,y,z)</code>. That's why I introduced for loop in <code>+ operator</code> overloading.</p>
  
  <p>Actually when it comes to appearance of codes, <code>Vector3</code> implementation looks better. But it cannot be scaled to different size vectors.</p>
  
  <p>Template's implementation looks less nicer than that of <code>Vector3</code>. But  template version can easily scaled to many size vectors by doing like this.</p>
  
  <pre><code>typedef GenericVector&lt;double, 3&gt; Vector3d;
  typedef GenericVector&lt;double, 6&gt; Vector6d;
  </code></pre>
  
  <p><strong>I'm not used to template of C++. Is there any one who can point problem of my implementation or understandings?</strong></p>
  
  <p>My question is that in implementation of <code>+ operator</code> in template version, is the choice of introducing for loop for accommodating unknown integer <code>N</code> <strong>right</strong> or <strong>the only choice</strong>? </p>
  
  <p>To simple one question, </p>
  
  <p><strong>Can I avoid for loop in <code>+ operator</code>?</strong></p>
  
  <p>This is parts of my codes. </p>
  
  <pre><code>template &lt;typename T, int N&gt;
  class GenericVector{
  protected:
      T data[N];
  public:
      GenericVector()
      {
          for(int i=0;i&lt;N;i++)
          {
              data[i] = static_cast&lt;T&gt;(0);
          }
      }
  
      GenericVector operator+(const GenericVector&lt;T,N&gt;&amp; vec) const{
          GenericVector temp = *this;
          for(int i=0;i&lt;N;i++)
          {
              temp.data[i] += vec.data[i];
          }
          return temp;
      }
  
      //...
  };
  </code></pre>