标签云

微信群

扫码加入我们

WeChat QR Code

I am trying to understand C++ move semantics, std::move() and rvalue references.I've implemented this example.#include <iostream>#include <vector>int main(){int totalSize = 6;std::vector<double> oldData{ 10, 20, 30 }; oldData.resize(totalSize, 0);std::vector<double> newData(std::move(oldData));return 0;}I'd like to know is this actually moving vector oldData to newData or is copying happening under the hood.


The move is fine, but think about your loop condition - oldData.size() keeps changing.

2019年04月25日55分23秒

how you populate the vector has nothing to do with the ctor and the move. That is just noise that distracts you.

2019年04月25日55分23秒

I concur with the previous comments about your example. A simple call to std::vector::resize will do exactly what you set out to do with the reserve and loop, only without a bug.

2019年04月25日55分23秒