Move C ++ 11 Constructor

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_;
    // Some definitions
    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?

+4
1

:

,

data_ = std::move(other.data_);

, , , , , , , . , =default .

, , (move) this->data_ , .

, this->data _ , -. , , , .

+2

Source: https://habr.com/ru/post/1683742/


All Articles