+ 4
Can you plz help me with std::move? Thank you already :)
Take a look at the code I tagged below, It demonstrates move semantics in C++. std::move function, as far as I know, changes the ownership of the data. But here, the string is in stack, and the vector is in heap, then how the compiler makes it work without copying? The newly added string has to be in the adjacent memory address, but the string to be added is somewhere else. Then how does the move semantics work better than copy semantics here? I mean, it has to copy the string in the adjacent memory right? How can it just "move"? https://code.sololearn.com/cPg7xtWloa1i/?ref=app
2 ответов
+ 2
If no exernal data (e.g. heap memory) is involved, than Move is indeed just Copy
(except for it changing the original)
But, while the string variable with all its pointers is in the stack, your string value may actually be stored in the heap (expecially if it's a long string)
So just let the compiler and the std libraries do their job, don't worry too much about it and use move semantics whenever you think you should
+ 1
Angelo tnx, have a nice day :)