Perfect forwarding describes a property of C++11 function templates which allows correctly deducing arguments as lvalues or rvalues and forwarding them in the same form to other functions.
Perfect forwarding was not possible in C++03 because template argument deduction cannot distinguish rvalues from const lvalues. See N1385: The forwarding problem for more details of the problem in C++03.
Support for rvalue-references and reference collapsing in C++11 enable perfect forwarding, so that generic call wrappers can accept any argument type and forward those arguments to another callable object, preserving whether the original arguments were lvalues or rvalues. I.e. perfect forwarding preserves the original argument's value category.
Perfect forwarding is done by taking arguments by "universal reference" (a function parameter of type T&&
where T
is a template parameter) and forwarding them using std::forward<T>
.
Use this tag for questions about writing C++11 functions to take any type of expression and questions about forwarding those arguments to other functions. Use move-semantics or rvalue-reference for more general questions.