Question

I want to assign just constructed unnamed(I mean "created in place without declaration". Fix me, please, if it isn't correct terminology.) container with a big number of elements to another container, using move assignment.

Should I write this way:

vector_used_before = std::move(vector<double>(100, 1.0));

Or simply:

vector_used_before = vector<double>(100, 1.0);

It's obvious that compiler can always use move assignment in this case, since we can't use created object anymore. But how does it really work?

Was it helpful?

Solution

vector<double>(100, 1.0) is already an rvalue, so these two snippets of code do exactly the same thing - move assignment. The correct answer is therefore "use the shortest one":

vector_used_before = vector<double>(100, 1.0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top