Rvalue Reference

An rvalue reference (T&&) is a C++ reference type that binds only a temporary (rvalue) objects.

It enables move semantics, transferring resources from short-lived temporaries instead of copying them.

Example

std::string s1 = "hello";
std::string s2 = std::move(c1); // 'c1' is now in a valid but unspecified state

Under the hood:

std::string(std::string&& other); // Move constructor

Here, other is an rvalue reference, it binds to the temporary returned by std::move(s1).

It’s important to note here that, other is an lvalue, but contains an rvalue reference. This is because its valid to take the address of other inside the move constructor.

Purpose / Benefits

  • Avoids expensive deep copies.
  • Enables move constructors and move assignment operators.
  • Allows efficient resource transfer (e.g. memory buffers, file handles).