Rvalue References

Here’s a new C++ feature: rvalue references.

An rvalue reference behaves just like an lvalue reference except that it can bind to a temporary (an rvalue), whereas you can not bind a (non const) lvalue reference to an rvalue.

A& a_ref3 = A(); // Error!
A&& a_ref4 = A(); // Ok

Question: Why on Earth would we want to do this?!

It turns out that the combination of rvalue references and lvalue references is just what is needed to easily code move semantics. The rvalue reference can also be used to achieve perfect forwarding, a heretofore unsolved problem in C++. From a casual programmer’s perspective, what we get from rvalue references is more general and better performing libraries.

I’m sure it will be a while before we see a compiler supporting it, but it’s a good concept to know.

More: Here’s the Forwarding Problem, which rvalue references solve (“perfect forwarding”). Via Boost.Range.

Published
Categorized as Software