What is std::move() and when should it be used?
Hello colleagues! I'm learning C++11 and trying to understand move semantics. I've read about std::move(), but I don't fully understand what it is and why it's needed.
For example, I have this code:
```cpp
std::vector createBigVector() {
std::vector v(1000000, 42);
return v;
}
int main() {
std::vector data = createBigVector(); // Copy or move?
}
```
I know that compilers can apply return value optimization (RVO/NRVO), but if I explicitly use std::move() in return, does it help or hinder?
Another example: I want to pass a large object to a function, but I don't need it anymore after passing. How to correctly use std::move()?
```cpp
void processData(std::vector data) {
// ... lots of work with data
}
int
No answers yet.
Log in to leave an answer
Blogs