# std::move vs std::forward and perfect forwarding

> What is the difference between std::move and std::forward? What is a forwarding reference and what does perfect forwarding solve?

Canonical: https://www.internhack.in/learn/interview/cpp-interview/cpp-std-move-vs-forward
Last updated: 2026-11-28

Difficulty: Advanced

## Question

What is the difference between std::move and std::forward? What is a forwarding reference and what does perfect forwarding solve?

## Answer

This question shows up when the interviewer wants to find your ceiling with C++11 features. The good news: once you see the problem being solved, the two utilities become easy to tell apart.

Start with std::move: it unconditionally casts its argument to an rvalue reference. It says 'I am done with this object, feel free to steal from it'. It does not need any template arguments from you and always yields an rvalue.

std::forward is conditional. It exists for generic code in which a function template receives an argument and wants to pass it along exactly as it was received: as an lvalue if the caller passed an lvalue, as an rvalue if the caller passed an rvalue. Think of a courier who must hand over the parcel exactly the way it was given: fragile stays fragile and disposable stays disposable, rather than everything being treated the same.

The setup is the forwarding reference (also called a universal reference). In a template such as 'template<class T> void wrap(T&& arg)', the parameter T&& is not an rvalue reference, because T is deduced. If you pass an lvalue of type int, T is deduced as int& and reference collapsing makes arg an int&. If you pass an rvalue, T is int and arg is an int&&. Note that this only works when T is a deduced template parameter of that very function; 'void f(std::vector<T>&&)' or a class template's member 'void g(T&&)' are plain rvalue references.

The trap is that inside the function, arg has a name, so it is an lvalue no matter what. If you forward it to another function naively as g(arg), the lvalue overload is always chosen and you lose the ability to move. Calling g(std::forward<T>(arg)) restores the original value category: the cast is to T&&, which collapses to an lvalue reference for lvalue arguments and an rvalue reference for rvalue arguments. That is perfect forwarding.

Where is it used in practice? Everywhere in the library: std::make_unique, std::make_shared, vector::emplace_back, std::thread's constructor and std::function all use it to pass constructor arguments through without extra copies. A sound rule of thumb is: use std::move on a concrete named object you are finished with; use std::forward on a forwarding-reference parameter, exactly once, at the point of the final hand-off.

A common mistake is to forward the same argument twice, since the first use may have moved from it. And std::forward without a template argument does not compile, unlike std::move, because it cannot deduce whether to be conditional.

## Code examples

### Forwarding preserves value category

```
#include <iostream>
#include <utility>

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

template <class T> void bad(T&& x)  { g(x); }                    // x is always an lvalue here
template <class T> void good(T&& x) { g(std::forward<T>(x)); }   // preserves category

int main() {
    int a = 0;
    bad(a);    // lvalue
    bad(1);    // lvalue (lost rvalue-ness!)
    good(a);   // lvalue
    good(1);   // rvalue
}
```

bad() treats every argument as an lvalue because named parameters are lvalues. good() uses std::forward to hand each argument on exactly as received.

## Key points

- std::move: unconditional cast to rvalue. std::forward<T>: conditional cast that preserves the caller's value category.
- T&& with deduced T is a forwarding reference, not an rvalue reference.
- Named parameters are lvalues even if their type is an rvalue reference.
- Reference collapsing: T& && becomes T&; T&& && becomes T&&.
- Use std::forward once, at the final hand-off, and never reuse the argument afterwards.

## Common follow-up questions

- Why is 'void f(std::vector<T>&&)' not a forwarding reference?
- What is reference collapsing?
- How does emplace_back avoid a copy compared with push_back?

## Interview tips

- Use the courier analogy for forward and the 'permission cast' description for move.
- Point to make_unique or emplace_back as real-world forwarding uses.

## Concepts covered

std::move, std::forward, perfect forwarding, forwarding reference, templates

Previous: [Copy elision, RVO and NRVO](https://www.internhack.in/learn/interview/cpp-interview/cpp-copy-elision-rvo) · Next: [Virtual functions, vtable and vptr](https://www.internhack.in/learn/interview/cpp-interview/cpp-virtual-functions-vtable) · [All of C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)

## More C++ & STL questions

- [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)
- [Operator overloading in C++](https://www.internhack.in/learn/interview/cpp-interview/cpp-operator-overloading)
- [Friend functions and inline functions](https://www.internhack.in/learn/interview/cpp-interview/cpp-friend-inline-functions)

## 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)
