Search code examples
c++move-semantics

Is it safe to assign new value to an object that has been moved from and use this object further?


std::string str1{"This is a test string"};
std::string str2{std::move(str1)};
str1 = "This is a new content of this string";
// Use str1...

Am I correct thinking that str1 is in valid state now and is totally safe to use after assigning new value to it?


Solution

  • Am I correct thinking that str1 is in valid state now and is totally safe to use after assigning new value to it?

    Yes, you can safely assign to str1(as you've done) and then use str1 for further purposes.