Opencv's documentation on cv::Mat
seems to indicate that there are no move constructors at the moment, so something like cv::Mat A=std::move(some_other_cv_mat)
doesn't make much sense. My current (and naïve) solution to this problem is to derive a class from cv::Mat
, for which I implement a move constructor as follows:
namespace cv
{
//matrix object derived from cv::Mat
class Mvbl_Mat final: public Mat
{
public:
//constructors
Mvbl_Mat(){};
Mvbl_Mat(int rows, int cols, int type, const Scalar &s):Mat(rows, cols, type, s){}
//destructor
~Mvbl_Mat(){};
//move constructor
Mvbl_Mat(Mvbl_Mat && other) noexcept
{
this->data=other.data;
other.data=nullptr;
}
//move assignment operator
Mvbl_Mat & operator=(Mvbl_Mat && other)
{
this->data=other.data;
other.data=nullptr;
return *this;
}
};
}
While this works for the limited problems that I face at the moment, there are obviously many limitations and the solution is far from ideal. So, what is the best way to emulate move semantics for cv::Mat
?
move
does not make much sense forcv::Mat
because the copy constructor does not copy the data. And your move constructor probably breaks the reference counter. – Earphonemove
makes a lot of sense, even, or especially for a refcount model (seeshared_ptr
). Imagine you'd like to pass aMat
into a function but you don't need theMat
for yourself after the call. Withoutmove
you are forced to hold on to your reference (and the memory) until the function returns. Withmove
you have the opportunity to reuse memory within the function in such cases. – Ruse