samedi 25 avril 2015

C++ 11 - Is moving non local variable safe?

Say I have a function which goes like:

void a_fct( std::vector<double> &some_vector )
{
  std::vector<double> a_temporary_vector = some_vector;

  ... some stuff involving only a_temporary_vector ...

  some_vector = a_temporary_vector;
}

which is of course a bit silly, but it is just intended as bringing the following general questions:

  • It seems to me that one should rather move a_temporary_vector into some_vector here, as it then goes out of scope. But is it safe to move some_vector into a_temporary_vector rather than copying ?

  • Imagine now that somewhere else in the code, I have a pointer to the vector given as argument here. Is this pointer still pointing to it if I move some_vector into a_temporary_vector ?

In both cases, can I expect it to be true for any STL implementation ?

Aucun commentaire:

Enregistrer un commentaire