Consider:
void g(int&);
void g(int&&);
template<class T>
void f(T&& x)
{
g(std::forward<T>(x));
}
int main()
{
f(10);
}
Since the id-expression x
is an lvalue, and std::forward
has overloads for lvalues and rvalues, why doesn't the call bind to the overload of std::forward
that takes an lvalue?
template<class T>
constexpr T&& forward(std::remove_reference_t<T>& t) noexcept;
It does bind to the overload of std::forward
taking an lvalue:
template <class T>
constexpr T&& forward(remove_reference_t<T>& t) noexcept;
It binds with T == int
. This function is specified to return:
static_cast<T&&>(t)
Because the T
in f
deduced to int
. So this overload casts the lvalue int
to xvalue with:
static_cast<int&&>(t)
Thus calling the g(int&&)
overload.
In summary, the lvalue overload of std::forward
may cast its argument to either lvalue or rvalue, depending upon the type of T
that it is called with.
The rvalue overload of std::forward
can only cast to rvalue. If you try to call that overload and cast to lvalue, the program is ill-formed (a compile-time error is required).
So overload 1:
template <class T>
constexpr T&& forward(remove_reference_t<T>& t) noexcept;
catches lvalues.
Overload 2:
template <class T> constexpr T&& forward(remove_reference_t<T>&& t) noexcept;
catches rvalues (which is xvalues and prvalues).
Overload 1 can cast its lvalue argument to lvalue or xvalue (the latter which will be interpreted as an rvalue for overload resolution purposes).
Overload 2 can can cast its rvalue argument only to an xvalue (which will be interpreted as an rvalue for overload resolution purposes).
Overload 2 is for the case labeled "B. Should forward an rvalue as an rvalue" in N2951. In a nutshell this case enables:
std::forward<T>(u.get());
where you are unsure if u.get()
returns an lvalue or rvalue, but either way if T
is not an lvalue reference type, you want to move the returned value. But you don't use std::move
because if T
is an lvalue reference type, you don't want to move from the return.
I know this sounds a bit contrived. However N2951 went to significant trouble to set up motivating use cases for how std::forward
should behave with all combinations of the explicitly supplied template parameter, and the implicitly supplied expression category of the ordinary parameter.
It isn't an easy read, but the rationale for each combination of template and ordinary parameters to std::forward
is in N2951. At the time this was controversial on the committee, and not an easy sell.
The final form of std::forward
is not exactly what N2951 proposed. However it does pass all six tests presented in N2951.