I knew there were probably already some similar questions on StackOverflow, but I didn't actually find anything that fully answered my doubts, so I asked my own question.
There are two different overloads of std::forward
:
template< class T >
T&& forward( typename std::remove_reference<T>::type& t );
template< class T >
T&& forward( typename std::remove_reference<T>::type&& t );
What is the difference with one universal reference template function using static_cast
? Like:
template<class T>
T&& myForward(T &&t) {
return static_cast<T&&>(t);
}
When I pass a rvalue of type A
, T
with be deduced to A
and myForward
will return A &&
; When a lvalue is passed, T
with be deduced to A &
, and it will return A &
. So what is the difference?
I understand the notion that the second overload can prohibit forwarding a rvalue to a lvalue, but why myForward
cannot?