0

I hope this isn't a silly question, I just wanted to make sure my understanding on this is clear.

If a parameter is received by RValue reference :

struct A { /* move and copy constructors here... */ };
template <class T> void f1(T&&) {

}
void demo()
{
    f1(A());
    f1(std::move<A>(A()));
}

It seems to me that it is passed by reference, and no move constructor is applied.

Is this true? I just wanted to make sure there is no copy elision or other optimization here.

Gonen I
  • 5,576
  • 1
  • 29
  • 60

1 Answers1

1

Correct. A constructor is only called of you do something with the argument that implicitly or explicitly constructs a new object.

The second line in your demo function is unnecessary: A() is already a prvalue.

Botje
  • 26,269
  • 3
  • 31
  • 41
  • 1
    I had prvalue before but miscorrected it to xvalue. [this post](https://stackoverflow.com/questions/3601602/what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues) agrees with you. – Botje Oct 05 '20 at 07:19