Biao Cao
Biao Cao

Reputation: 151

what will happen when std::move with c style array or not movable object

For a C style array, what will happen when I run float *fp1 = std::move(fp);, as seen in the following code marked (1)? Is it the same as float *fp1 = fp;, as seen in the following code marked (2)?

I printed the results and it seems they are the same. In general, std::move will do nothing if the object is not movable, right?

int main()
{
    float *fp = new float[20];
    //float *fp1 = std::move(fp); //(1)
    float *fp1 = fp; //(2)
    
    std::cout << "fp: " << fp << " fp1: "<< fp1 << std::endl;
    unique_ptr<float> u_fp(fp);
    cout << "u_fp : " << u_fp.get() << endl;
    unique_ptr<float> u_fp1 = std::move(u_fp);
}

Upvotes: 1

Views: 1917

Answers (2)

MvG
MvG

Reputation: 60958

To be generally, std::move will do nothing if the object is not movable, right?

std::move is just a cast to rvalue reference, expressing that you're OK loosing the value in its argument. It's up to the constructor or assignment operator to do something special with that. In general you can expect the argument of std::move to be in a valid but otherwise undefined state after that, so executing its destructor or resetting it should both be fine, but other than that who knows.

Since a simple data type like a pointer has no overloaded constructor or assignment operator, the cast will have no effect here, and you indeed get a plain assignment.

I'd use the term "movable type" as a superset of "copyable". Any type that up can copy you can move by copying it then getting rid of the original. Some types you can move but not copy; for those std::move is particularly relevant. And some types you can't even move, they will have that constructor marked deleted or private. cppreference agrees by stating that a copyable type is movable as well.

Also note that many container types are copyable (if their elements are) but copying them is more expensive than moving them. So it would be wrong to say that std::move is only relevant for non-copyable movable types.

Upvotes: 1

Serge Ballesta
Serge Ballesta

Reputation: 149075

With float *fp = new float[20];, fp is not an array but is a pointer to the first element of an array of 20 float objects. You can display sizeof(fp) to make sure.

A pointer is indeed a moveable type, simply moving or copying it is exactly the same operation. A non moveable type is a type where the move assignment is forbidden, either by explictely deleting the move assignment operator, or because moving it would move a non moveable sub-object. Trying to move a non moveable object will result in a compilation error.

BTW, if you try to move a true C array, it will decay to a pointer, and it will be the same as first case:

float arr[20];
float *fp1 = std::move(arr);  // arr decays to a pointer so the same as fp1 = arr

Upvotes: 4

Related Questions