title: Passing a functor as C++ template parameter
body: <p>As an exercise for my personal enlightenment, I implement vector math with expression templates. I want to implement some operations that apply the same unary function to all elements to a vector expression. So far, I do this.</p>
  
  <p>My base vector expression template is implemented like this</p>
  
  <pre><code>template &lt;typename E&gt;
  class VectorExpr {
  public:
    int size() const { return static_cast&lt;E const&amp;&gt;(*this).size(); }
  
    float operator[](int i) const { return static_cast&lt;E const&amp;&gt;(*this)[i]; }
  
    operator E&amp; () { return static_cast&lt;E&amp;&gt;(*this); }
  
    operator E const&amp; () const { return static_cast&lt;const E&amp;&gt;(*this); }
  }; // class VectorExpr
  </code></pre>
  
  <p>Then, an object supposed to be a vector will look like this</p>
  
  <pre><code>class Vector2 : public VectorExpr&lt;Vector2&gt; {
  public:
      inline size_t size() const { return 2; }
  
      template &lt;typename E&gt;
      inline Vector2(VectorExpr&lt;E&gt; const&amp; inExpr) {
      E const&amp; u = inExpr;
      for(int i = 0; i &lt; size(); ++i)
          mTuple[i] = u[i];
     }
  
  private:
     float mTuple[2];
  };
  </code></pre>
  
  <p>Let's say I want to apply std::sin to all elements of an expression</p>
  
  <pre><code>template &lt;typename E&gt;
  class VectorSin : public VectorExpr&lt;VectorSin&lt;E&gt; &gt; {
      E const&amp; mV;
  
  public:
      VectorSin(VectorExpr&lt;E&gt; const&amp; inV) : mV(inV) {}
  
      int size() const { return mV.size(); }
  
      float operator [] (int i) const { return std::sin(mV[i]); }
  };
  </code></pre>
  
  <p>Question => If I want to add more functions, I copy-paste what I do for the sin function, for every single function (like cos, sqrt, fabs, and so on). How I can avoid this kind of copy-pasting ? I tried things and figured out I'm still low in template-fu. No boost allowed ^^</p>