mercredi 3 mars 2021

Move of local variable that have move c-tor into function with parameter by value

Trying to clear my understanding when move happens, and when it's better to write functions that pass values by value, instead of reference, to gain from moves.

void foo()
{
  std::string localStr;
//
  foo1(localStr);
}
void foo1(const std::string str)
{

}

Will in this case localStr be moved into foo1?

And if to add one more level of functions call:

void foo()
{
  std::string localStr;
//
  foo1(localStr);
  //localStrisn't used anymore
}
void foo1(const std::string str)
{
   foo2(str);
   //str isn't used anymore
}
void foo2(const std::string str)
{
}

Will there be two moves?

Aucun commentaire:

Enregistrer un commentaire