jeudi 30 juillet 2020

When are function arguments copied?

I am interested to know when the arguments of a function are copied.

#include <vector>

void foo (std::vector<float> a)
{
   std::vector<float> y = std::move(a);
   //do something with vector y
}

int main()
{
   std::vector<float> x {1.0f, 2.0f, 3.0f};
   std::cout << x.at(0) << " " << x.at(1) << " " << x.at(2) << std::endl; //1.0 2.0 3.0
   foo(x);
   std::cout << x.at(0) << " " << x.at(1) << " " << x.at(2) << std::endl; //print nothing
}

Does the function copy its argument from the start? If not, how do we know when the arguments are copied? From the above code, I assume that the arguments are not copied because std::move still affects variable x.

Aucun commentaire:

Enregistrer un commentaire