Std :: move on the stack

I know this is a very simple, maybe even awkward question, but it's hard for me to figure it out. If I std :: move from something onto the stack to another object, can another object still be used when the original goes out of scope?

#include <iostream> #include <string> int main(int argc, char* argv[]) { std::string outer_scope; { std::string inner_scope = "candy"; outer_scope = std::move(inner_scope); } std::cout << outer_scope << std::endl; return 0; } 

Is outer_scope still valid when I try to print it?

+5
source share
1 answer

Yes, this is still true, the innerscope object loses the ownership of the contents that it had previously, and the violator becomes the owner. std :: move is a vector swap. If you change the external and internal, the destructive internal element will not affect the content that now belongs to the external.

+4
source

Source: https://habr.com/ru/post/1257746/


All Articles