Share via


forward

Conditionally casts its argument to an rvalue reference if the argument is an rvalue or rvalue reference. This restores the rvalue-ness of an argument to the forwarding function in support of perfect forwarding.

template<class Type>    // accepts lvalues
Type&& forward(typename remove_reference<Type>::type& Arg)

template<class Type>    // accepts everything else
Type&& forward(typename remove_reference<Type>::type&& Arg) noexcept

Parameters

Parameter

Description

Type

The type of the value passed in Arg, which might be different than the type of Arg. Typically determined by a template argument of the forwarding function.

Arg

The argument to cast.

Return Value

Returns an rvalue reference to Arg if the value passed in Arg was originally an rvalue or a reference to an rvalue; otherwise, returns Arg without modifying its type.

Remarks

You must specify an explicit template argument to call forward.

forward does not forward its argument. Instead, by conditionally casting its argument to an rvalue reference if it was originally an rvalue or rvalue reference, forward enables the compiler to perform overload resolution with knowledge of the forwarded argument's original type. The apparent type of an argument to a forwarding function might be different than its original type—for example, when an rvalue is used as an argument to a function and is bound to a parameter name; having a name makes it an lvalue, regardless of whether the value actually exists as an rvalue—forward restores the rvalue-ness of the argument.

Restoring the rvalue-ness of an argument's original value in order to perform overload resolution is known as perfect forwarding. Perfect forwarding enables a template function to accept an argument of either reference type and to restore its rvalue-ness when it's necessary for correct overload resolution. By using perfect forwarding, you can preserve move semantics for rvalues and avoid having to provide overloads for functions that vary only by the reference type of their arguments.

Requirements

Header: <utility>

Namespace: std

See Also

Reference

<utility>

Lvalues and Rvalues