Set openCV basic structures to zero in a unique way For test cases I need a unique way to set any structure to zero. For openCV I wrote this:

  // Fill every basic structure of openCV with zero.
  template &lt; typename Tp &gt;
  void zero( cv::Point_&lt;Tp&gt;&amp; p ) {
    p.x = p.y = 0;
  }
  template &lt; typename Tp &gt;
  void zero( cv::Point3_&lt;Tp&gt;&amp; p ) {
    p.x = p.y = p.z = 0;
  }
  template &lt; typename Tp &gt;
  void zero( cv::Size_&lt;Tp&gt;&amp; s ) {
    s.width = s.height = 0;
  }
  template &lt; typename Tp &gt;
  void zero( cv::Rect_&lt;Tp&gt;&amp; r ) {
    r.x = r.y = r.width = r.height = 0;
  }
  inline void zero( cv::RotatedRect&amp; r ) {
    zero( r.center );
    zero( r.size );
    r.angle = 0;
  }
  inline void zero( cv::TermCriteria&amp; t ) {
    t.type = t.maxCount = 0;
    t.epsilon = 0.0;
  }
  template &lt; typename Tp, int M, int N&gt;
  void zero( cv::Matx&lt;Tp, M, N&gt;&amp; m ) {
    m = 0;
  }
  // Vec&lt;&gt; and Scalar&lt;&gt; are derived from Matx&lt;&gt;
  inline void zero( cv::Range&amp; r ) {
    r.start = r.end = 0;
  }
  inline void zero( cv::Mat&amp; m ) {
    m = 0;
  }
  inline void zero( cv::SparseMat&amp; sm ) {
    sm.clear();
  }
  template &lt; typename Tp &gt;
  void zero( cv::SparseMat_&lt;Tp&gt;&amp; sm ) {
    sm.clear();
  }


Setting cv::Mat to zero by assigning a new zero filled Mat is reallocating. But looping over would be slow (now done by *= 0). Using memset( m.data, ... ) is only possible if m.isContinuous(). Any idea of being safe and fast? Is it possible to join some derived cases?

The template parameters _Tp has been replaced by Tp because starting with _, followed by uppercase letter is reserved. That is something the openCV authors do violate too (I copied it from the openCV core code).

Mat, Matx and derived Vec and Scalar can be set to zero by multiplying with zero. Internal this is a for() over all elements but without the realloc of the zeros() assignment.
If you want to zero a Matx, you can take advantage of its val public data member, which is a good old C array. Then you can rely on the standard library algorithm std::fill to set every element of the matrix to 0; the algorithm generally does static dispatch at compile time to call std::memset whenever possible. Using it should allow you to always be safe but also to be safe when possible:

template &lt; typename Tp, int M, int N&gt;
void zero( cv::Matx&lt;Tp, M, N&gt;&amp; m ) {
  std::fill(std::begin(m.val), std::end(m.val), 0);
}


Since Matx::val is a fixed-size C array, std::begin and std::end should work. If you don't use a C++11 standard library, you can implement it as follows instead:

template &lt; typename Tp, int M, int N&gt;
void zero( cv::Matx&lt;Tp, M, N&gt;&amp; m ) {
  std::fill(m.val, m.val + M*N, 0);
}


For cv::Mat, the documentation says that:


  To set all the matrix elements to the particular value after the construction, use the assignment operator Mat::operator=(const Scalar&amp; value).


It means that you can zero the matrix like this (I couldn't find equivalent overloads for Matx or SparseMat):

inline void zero( cv::Mat&amp; m ) {
  m = 0;
}


Also, as a side note, most modern compilers just don't care about inline, at least they don't take it as a hint for inlining since they already do a great job when it comes to inlining stuff. Nowadays, inline is just a way to solve ODR problems, which don't happen with templates. All of your functions are function templates, so you can safely remove inline.