移动语义和 cv::Mat
move semantics and cv::Mat
Opencv's documentation on cv::Mat
似乎表明目前没有移动构造函数,所以像 cv::Mat A=std::move(some_other_cv_mat)
这样的东西没有多大意义。我目前(和天真的)解决这个问题的方法是从 cv::Mat
派生一个 class,为此我实现了一个移动构造函数,如下所示:
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;
}
};
}
虽然这适用于我目前面临的有限问题,但显然存在很多限制,而且解决方案远非理想。那么,模拟 cv::Mat
移动语义的最佳方法是什么?
没有必要这样做。 cv::Mat
的复制构造函数实际上并没有复制数据。它基本上是一个引用,所有对象共享相同的数据。
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters
m
Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m . If you want to have an independent copy of the sub-array, use Mat::clone()
截至 4.x OpenCV 提供 Mat (Mat &&m)
and Mat & operator= (Mat &&m)
。
如果您使用的是 4.x 之前的版本,我建议您查看 cv::Mat
中定义的移动构造函数和移动赋值运算符实现,modules/core/include/opencv2/core/mat.inl.hpp
,因为它比复制 .data
成员要复杂一些。
Opencv's documentation on cv::Mat
似乎表明目前没有移动构造函数,所以像 cv::Mat A=std::move(some_other_cv_mat)
这样的东西没有多大意义。我目前(和天真的)解决这个问题的方法是从 cv::Mat
派生一个 class,为此我实现了一个移动构造函数,如下所示:
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;
}
};
}
虽然这适用于我目前面临的有限问题,但显然存在很多限制,而且解决方案远非理想。那么,模拟 cv::Mat
移动语义的最佳方法是什么?
没有必要这样做。 cv::Mat
的复制构造函数实际上并没有复制数据。它基本上是一个引用,所有对象共享相同的数据。
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Parameters
m
Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m . If you want to have an independent copy of the sub-array, use Mat::clone()
截至 4.x OpenCV 提供 Mat (Mat &&m)
and Mat & operator= (Mat &&m)
。
如果您使用的是 4.x 之前的版本,我建议您查看 cv::Mat
中定义的移动构造函数和移动赋值运算符实现,modules/core/include/opencv2/core/mat.inl.hpp
,因为它比复制 .data
成员要复杂一些。