title: C++: How can I create dynamic template type
body: <p>I have the following piece of code, which I created for changing the intensity of a pixel in an OpenCV image (Cv::Mat class).</p>
  
  <p>As you can see, I'm looping in both cases, but with different template type. </p>
  
  <p>The 'transfer' function can be overloaded.</p>
  
  <p>My question is, therefore, how can I create dynamic template type so that it looks better ..</p>
  
  <pre><code>Mat mat = _mat.clone() ;
  int channels = mat.channels();
  
  switch(channels)
  {
  case 1: 
      for (int i=0; i&lt;mat.rows; i++)
      {
          for (int j=0; j&lt;mat.cols; j++)
          {
              uchar src = mat.at&lt;uchar&gt;(i,j);
              uchar dst = mat.at&lt;uchar&gt;(i,j);
  
              t.transfer(src, dst);
          }
      }
      break;
  
  case 3: 
      for (int i=0; i&lt;mat.rows; i++)
      {
          for (int j=0; j&lt;mat.cols; j++)
          {
              Vec3b src = mat.at&lt;Vec3b&gt;(i,j);
              Vec3b dst = mat.at&lt;Vec3b&gt;(i,j);
  
              t.transfer(src, dst);
          }
      }
      break;
  }
  
  return mat ;
  </code></pre>