Difficulty: Advanced
What is the difference between std::move and std::forward? What is a forwarding reference and what does perfect forwarding solve?
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.
#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.
std::move, std::forward, perfect forwarding, forwarding reference, templates