Use of const Rvalue References

Let’s learn when to use const rvalue references.

When to use const rvalue references?

An lvalue overload can accept both lvalues and rvalues, but an rvalue overload can only take rvalues.

The goal of rvalue references is sparing copies and using move semantics. At the same time, we cannot move away from const values. Therefore the usage of const rvalue references communicates that a given operation is only supported on rvalues, yet we can still make a copy because we cannot move.

What is important to note is that f(const T&&) can take both T&& and const T&&, while f(T&&) can only take the non-const rvalue reference.

Therefore if we want to stop using rvalue references completely, we need to delete the f(const T&&) overload.

What would happen otherwise?

If we delete the non-const overload, the compilation will fail with rvalue references. Although it doesn’t make much sense in general to pass const rvalue references, the code will still compile.

Get hands-on with 1400+ tech skills courses.