# Lvalues, rvalues and move semantics

> What are lvalues and rvalues? What are move semantics and why did C++11 introduce rvalue references?

Canonical: https://www.internhack.in/learn/interview/cpp-interview/cpp-lvalue-rvalue-move-semantics
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

What are lvalues and rvalues? What are move semantics and why did C++11 introduce rvalue references?

## Answer

Move semantics is probably the single biggest performance feature C++11 gave us, and it rests on one distinction: is this object something that will keep existing, or is it about to disappear? Let's build up to it.

A classic informal rule: an lvalue has a name and an address you can take, so it persists beyond the current expression (a variable, an array element, a dereferenced pointer). An rvalue is a temporary, usually without a name, that is about to be destroyed (the result of a + b, a literal like 42, a function returning by value). Formally, C++11 refines this into lvalues, xvalues and prvalues, but the simple version is enough to reason with: 'can I take its address?' If yes, it is an lvalue.

Before C++11, passing or returning a large object by value meant a deep copy, even when the source was a temporary that would be thrown away immediately. That is like photocopying a 500-page report to hand to a colleague while you are about to shred the original. Move semantics lets the destination simply take over the source's internals: a vector's move constructor just copies three pointers and nulls the source, an O(1) operation instead of an O(n) copy.

To make this expressible, C++11 introduced the rvalue reference T&&, which binds to temporaries. Overloading on it lets a class provide both a copy constructor T(const T&) for lvalues and a move constructor T(T&&) for rvalues. The compiler picks the right one automatically. When you have an lvalue that you know you no longer need, you can call std::move(x). Despite the name, std::move does not move anything: it is simply a cast to an rvalue reference, giving permission for a move constructor or assignment to pull resources out of x.

Important subtleties. A named rvalue reference is itself an lvalue inside the function: given 'void f(int&& r)', the expression r is an lvalue, so calling g(r) picks the lvalue overload unless you write g(std::move(r)). After a move, the source object is in a valid but unspecified state; you may destroy it or assign a new value to it, but should not rely on its contents. Also, do not std::move a const object (it silently falls back to a copy) and do not std::move a local in a return statement, since it can block copy elision.

In practice you benefit from moves without writing anything: standard containers and strings are move-enabled, returning a vector from a function is cheap, and vector::push_back(std::move(s)) avoids a string copy. When you write your own resource-owning class, provide move operations, mark them noexcept, and follow the rule of five.

## Code examples

### Overload resolution with lvalues and rvalues

```
#include <iostream>
#include <string>
#include <utility>
#include <vector>

void f(int&)  { std::cout << "lvalue\n"; }
void f(int&&) { std::cout << "rvalue\n"; }

int main() {
    int x = 1;
    f(x);              // lvalue
    f(2);              // rvalue (literal)
    f(std::move(x));   // rvalue (cast)
    int&& r = 3;
    f(r);              // lvalue: a named rvalue reference is an lvalue

    std::string s = "a fairly long string that lives on the heap, probably";
    std::vector<std::string> v;
    v.push_back(std::move(s));   // steals the buffer instead of copying
    std::cout << v[0].size() << "\n";
    std::cout << (s.empty() ? "moved-from is empty (typical)" : "not empty") << "\n";
}
```

The moved-from string is valid but its contents are unspecified; emptying it is what implementations do in practice. Never depend on it.

## Key points

- lvalue: has identity and persists; rvalue: temporary about to expire.
- T&& binds to rvalues and enables move constructors and move assignment.
- std::move is just a cast to an rvalue reference; it moves nothing by itself.
- A named rvalue reference parameter is an lvalue inside the function.
- Moved-from objects are valid but unspecified; only destroy or reassign them.

## Common follow-up questions

- What is an xvalue?
- Why does std::move on a const object result in a copy?
- Why is 'return std::move(local);' usually a bad idea?

## Interview tips

- Explain std::move as 'a cast that grants permission', not as an action.
- Use the vector move (three pointers) to show O(1) versus O(n).

## Concepts covered

lvalue, rvalue, move semantics, rvalue reference

Previous: [Rule of three, rule of five and rule of zero](https://www.internhack.in/learn/interview/cpp-interview/cpp-rule-of-three-five-zero) · Next: [Copy elision, RVO and NRVO](https://www.internhack.in/learn/interview/cpp-interview/cpp-copy-elision-rvo) · [All of C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)

## More C++ & STL questions

- [std::move vs std::forward and perfect forwarding](https://www.internhack.in/learn/interview/cpp-interview/cpp-std-move-vs-forward)
- [Virtual functions, vtable and vptr](https://www.internhack.in/learn/interview/cpp-interview/cpp-virtual-functions-vtable)
- [Pure virtual functions and abstract classes](https://www.internhack.in/learn/interview/cpp-interview/cpp-pure-virtual-abstract-classes)
- [Why do base classes need a virtual destructor?](https://www.internhack.in/learn/interview/cpp-interview/cpp-virtual-destructor)
- [Object slicing](https://www.internhack.in/learn/interview/cpp-interview/cpp-object-slicing)
- [Function overloading vs overriding (and name hiding)](https://www.internhack.in/learn/interview/cpp-interview/cpp-overloading-vs-overriding)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)
