mardi 5 avril 2016

string move assignment exchange of values

I was programming some test cases an noticed an odd behaviour.

An move assignment to a string did not erase the value of the first string, but assigned the value of the target string.

sample code:

#include <utility>
#include <string>
#include <iostream>

int main(void) {
  std::string a = "foo";
  std::string b = "bar";
  std::cout << a << std::endl;
  b = std::move(a);
  std::cout << a << std::endl;

  return 0;
}

result:

$ ./string.exe
foo
bar

expected result:

$ ./string.exe
foo

So to my questions:

  • Is that intentional?
  • Does this happen only with strings and/or STL objects?
  • Does this happen with custom objects (as in user defined)?

Environment: Win10 64bit msys2 g++ 5.2

Aucun commentaire:

Enregistrer un commentaire