title: c++ - Implement template operator non-friend
body: <p>I have a simple generice arithmetic vector class and want to implement the * operator for scalar multiplication:</p>
  
  <pre><code>template&lt;class Value_T, unsigned int N&gt;
  class VectorT
  {
  public:
      typedef Value_T value_type;
      typedef unsigned int size_type;
  
      size_type GetNumElements() const { return N; }      
  
      // class member
      // elements in the vector
      value_type elements[N];     
  
      // the number of elements
      static const size_type size = N;
  };
  
  // scalar multiplication
  template&lt;class Value_T, unsigned int N&gt;
  const VectorT&lt;Value_T, N&gt; operator*(const VectorT&lt;Value_T, N&gt;&amp; v, Value_T s)        
  {
      VectorT&lt;Value_T,N&gt; vTemp(v);
      for (unsigned int i = 0; i &lt; v.GetNumElements(); i++)
      {
          vTemp.elements[i] *= s;
      }
      return vTemp;
  }
  </code></pre>
  
  <p>Using it like this ...</p>
  
  <pre><code>typedef VectorT&lt;double, 3&gt; Vec3;
  
  int main(int argc, char* argv[])
  {
      Vec3 v;
      Vec3 v2 = v * 2; // multiply with scalar int
      return 0;
  }
  </code></pre>
  
  <p>... gives compiler error C2782 (MSVC2012) that the template parameter Value_T for the * operator is ambiguous: int or double.</p>
  
  <p>If I define the *operator within my class as friend function the error is gone and it works fine for scalar int or double. But actually there is no need here for friend declaration as the public interface of class VectorT is sufficient for the operator (In my real code I have the members privat and some public accessor methods).</p>
  
  <p>I want the scalar multiplication work only for the Value_T type: For a VectorT&lt;int, N&gt; I want only integer values be given to the *operator. I further want the *operator being commutativ. That's why I don't implement it as a simple member function, where the lefthand operator is always of type VectorT. </p>
  
  <p>How to implement * operator as non-member and non-friend here?</p>