This sample is licensed under the CC0 Public Domain Dedication.
Forward arguments of one function to another as though the wrapped function had been called directly.
Perfect forwarding allows us to preserve an argument’s value
category (lvalue/rvalue) and
volatile modifiers. Perfect
forwarding is performed in two steps: receive a forwarding
reference (also known as universal reference), then forward it
In our example, the arguments
u on line 4 are forwarding
references because they are declared in the form
is a template parameter. We use
std::forward on lines 6–7 to forward
these arguments to
allowing them to be moved into the pair when the original argument
was an rvalue expression.
Perfect forwarding is often used with variadic templates
to wrap calls to functions with an arbitrary number of arguments.
use perfect forwarding to forward their arguments to the
constructor of the wrapped type.