Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a compiler automatically move a function argument if the function call is the return statement?

In the following situation can a compiler automatically move the function argument v or does it have to be declared manually?

std::vector Filter(std::vector v);

void DoSomeStuffAndCallFilter(std::vector v)
{
  // do some stuff to v

  // can the compiler automatically std::move v in this call?
  // ie. return Filter(std::move(v));
  //
  return Filter(v);
}
like image 897
fun4jimmy Avatar asked May 21 '14 10:05

fun4jimmy


1 Answers

In your case, the compiler can do so as an allowed optimisation under the as-if rule, because it knows the destructor and copy-constructor of your std::vector intimately, and can thus prove there is no difference to the observable behavior.

Still, it is a "quality of implementation issue", and depends on heavy optimisations being done.

like image 178
Deduplicator Avatar answered Sep 30 '22 02:09

Deduplicator