I am looking at some code that I have inherited and it has a matrix class which implements 2D matrices in C++ and has move constructors and assignment operator.
The way it is implemented is as follows:
template<typename T, int rows, int cols>
class matrix_data {
...
std::unique_ptr<T[]> data_;
// Some definitions
typedef matrix_data<T, rows, cols> this_type
matrix_data(this_type && other)
{
std::swap(data_, other.data_);
}
};
Now, I am not sure why the data pointers are being swapped here. I thought it should be something like
data_ = std::move(other.data_);
I am guessing with the swap it is still ok because the other
instance should be in an invalid state anyway after the move.
My question is whether I can replace the statement with data_ = std::move(other.data_);
Is there some unique_ptr
deletion stuff that is the reason for doing the swap instead of the move i.e. if I do the move would the original data be deleted correctly?
Aucun commentaire:
Enregistrer un commentaire