Taking an const
lvalue reference and taking an rvalue reference are two different things.
Similarities:
- Neither will cause an copy or move to take place because they are both references. A reference just references an object, it doesn't copy/move it in any way.
Differences:
A const
lvalue reference will bind to anything (lvalue or rvalue). An rvalue reference will only bind to non-const
rvalues - much more limited.
The parameter inside the function cannot be modified when it is a const
lvalue reference. It can be modified when it's an rvalue reference (since it is non-const
).
Let's look at some examples:
Taking const
lvalue reference: void f(const T& t);
Passing an lvalue:
T t; f(t);
Here, t
is an lvalue expression because it's the name of the object. A const
lvalue reference can bind to anything, so t
will happily be passed by reference. Nothing is copied, nothing is moved.
Passing an rvalue:
f(T());
Here, T()
is an rvalue expression because it creates a temporary object. Again, a const
lvalue reference can bind to anything, so this is okay. Nothing is copied, nothing is moved.
In both of these cases, the t
inside the function is a reference to the object passed in. It can't be modified by the reference is const
.
Taking an rvalue reference: `void f(T&& t);
Passing an lvalue:
T t;
f(t);
This will give you a compiler error. An rvalue reference will not bind to an lvalue.
Passing an rvalue:
f(T());
This will be fine because an rvalue reference can bind to an rvalue. The reference t
inside the function will refer to the temporary object created by T()
.
Now let's consider std::move
. First things first: std::move
doesn't actually move anything. The idea is that you give it an lvalue and it turns it into an rvalue. That's all it does. So now, if your f
takes an rvalue reference, you could do:
T t;
f(std::move(t));
This works because, although t
is an lvalue, std::move(t)
is an rvalue. Now the rvalue reference can bind to it.
So why would you ever take an rvalue reference argument? In fact, you shouldn't need to do it very often, except for defining move constructors and assignment operators. Whenever you define a function that takes an rvalue reference, you almost certainly want to give a const
lvalue reference overload. They should almost always come in pairs:
void f(const T&);
void f(T&&);
Why is this pair of functions useful? Well, the first will be called whenever you give it an lvalue (or a const
rvalue) and the second will be called whenever you give it a modifiable rvalue. Receiving an rvalue usually means that you've been given a temporary object, which is great news because that means you can ravage its insides and perform optimizations based on the fact that you know it's not going to exist for much longer.
So having this pair of functions allows you to make an optimization when you know you're getting a temporary object.
There's a very common example of this pair of functions: the copy and move constructors. They are usually defined like so:
T::T(const T&); // Copy constructor
T::T(T&&); // Move constructor
So a move constructor is really just a copy constructor that is optimized for when receiving a temporary object.
Of course, the object being passed isn't always a temporary object. As we've shown above, you can use std::move
to turn an lvalue into an rvalue. Then it appears to be a temporary object to the function. Using std::move
basically says "I allow you to treat this object as a temporary object." Whether it actually gets moved from or not is irrelevant.
However, beyond writing copy constructors and move constructors, you'd better have a good reason for using this pair of functions. If you're writing a function that takes an object and will behave exactly the same with it regardless of whether its a temporary object or not, simply take that object by value! Consider:
void f(T t);
T t;
f(t);
f(T());
In the first call to f
, we are passing an lvalue. That will be copied into the function. In the second call to f
, we are passing an rvalue. That object will be moved into the function. See - we didn't even need to use rvalue references to cause the object to be moved efficiently. We just took it by value! Why? Because the constructor that is used to make the copy/move is chosen based on whether the expression is an lvalue or an rvalue. Just let the copy/move constructors do their job.
As to whether different argument types result in the same code - well that's a different question entirely. The compiler operates under the as-if rule. This simply means that as long as the program behaves as the standard dictates, the compiler can emit whatever code it likes. So the functions may emit the same code if they happen to do exactly the same thing. Or they may not. However, it's a bad sign if you're functions that take a const lvalue reference and an rvalue reference are doing the same thing.