I am considering some code that I have inherited, and it has a matrix class that implements 2D matrices in C ++ and has movement constructors and an assignment operator.
Implementation Method:
template<typename T, int rows, int cols>
class matrix_data {
...
std::unique_ptr<T[]> data_;
typedef matrix_data<T, rows, cols> this_type
matrix_data(this_type && other)
{
std::swap(data_, other.data_);
}
};
Now I'm not sure why the data pointers are reversed here. I thought it should be something like
data_ = std::move(other.data_);
I assume that using swap is still fine, because the instance othermust be in an invalid state anyway after the move.
My question is, can I replace the operator data_ = std::move(other.data_);Is there any delete element unique_ptrthat is causing swap to execute instead of moving, i.e. if I do this step, will the original data be deleted correctly?