Set openCV basic structures to zero in a unique way
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.