I am curious why the below implementation is wrong?
template<typename T> // For lvalues (T is T&),
T&& std::forward(T&& param) // take/return lvalue refs.
{ // For rvalues (T is T),
return static_cast<T&&>(param); // take/return rvalue refs.
}
According to "Effective Modern C++" book, this is the possible implementation of std::forward
which is understandable.
template<typename T>
T&& forward(remove_reference_t<T>& param)
{
return static_cast<T&&>(param);
}
A user in the comments here answered like this:
In the first case, you could get away with std::forward(x), but in the second you must explicitly provide the template parameters, as they cannot be inferred.
Questions
- Why in the 1st case writing
std::forward(x)
is possible, but for the 2nd case it is not? I.e., why in the 2nd case type deduction won't work? - What is wrong with writing
std::forward(x)
? I understand that it doesn't match with the standard'sstd::forward<T>(x)
signature, but then another questions comes: why we need to specify type explicitly here?