面试题:C++中std::move使用的常见误区之性能问题
在C++中,有人认为使用std::move一定能提升性能。请分析以下代码,说明其中使用std::move是否一定能提升性能,并阐述原因。
```cpp
#include <iostream>
#include <vector>
class MyClass {
public:
std::vector<int> data;
MyClass() = default;
MyClass(const MyClass& other) : data(other.data) {
std::cout << "Copy constructor called" << std::endl;
}
MyClass(MyClass&& other) noexcept : data(std::move(other.data)) {
std::cout << "Move constructor called" << std::endl;
}
};
MyClass createMyClass() {
MyClass obj;
for (int i = 0; i < 1000; ++i) {
obj.data.push_back(i);
}
return std::move(obj);
}
```