lundi 4 juillet 2016

std::move of *this and later access to class methods and fields

I understand it is something stupid to do with code... but for the sake of understanding, please consider the following:

#include <iostream>
#include <memory>
#include <utility>

struct S;
void f( S && s );

struct S {
  S() : i{std::vector<int>(10, 42)} {}
  std::vector<int> i;
  void call_f() { f( std::move(*this) ); }
  void read() { std::cout << " from S: " <<  i.at(3) << std::endl; }
};

void f(S && s)  { std::cout << " from f: " <<  s.i.at(3) << std::endl; }

int main() {
  S s;
  s.call_f();
  s.read();
}

This compiles and run for both g++ and clang++, while I would expect that the std::vector<int> to be moved. Running this in gdb and looking at the memory shows that the address of s.i is not set to zero after the std::move, while I expected that for non-POD types it would. Therefore, I expected a segfault from this piece of code.
Could please anyone explain me this behaviour? Why is not neither s nor its inner field invalidated? Is it a feature with this?

Aucun commentaire:

Enregistrer un commentaire