Kemin Zhou
Kemin Zhou

Reputation: 6891

Is C++ build in array movable

In C++, is the primitive array movable? Say we used a simple array to store some object as a member of a class.

class Witharray {
    public:
       Witharray(Witharray&& o) : arrmem(std::move(o.arrmem)) { }

    private:
       Myobj  arrmem[4];
};

You will get "error: array used as initializer." We could move individual elements. Any one has some comments on this. Maybe we should be using vectors; however, for a small array of fixed size, raw array may be more efficient. I have searched this forum and did not find any answer to this question.

Upvotes: 0

Views: 169

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283733

The defaulted move constructor will do the right thing.

Witharray(Witharray&&) = default;

will move the elements of the array member subobject.

If you have some elements for which defaulted move isn't sufficient, you can use a subobject (for either the array or the other elements) to provide custom behavior for some and default others.

Upvotes: 2

Related Questions