I'm learning about templates and particularly std::forward
; when I check its implementation it uses another class template std::remove_reference
in its arguments list:
template<class _Ty>
_Ty&& forward(typename remove_reference<_Ty>::type& _Arg)
{// forward an lvalue as either an lvalue or an rvalue
// ...
}
btw: I've removed a few things like inline, constexpr, NO_EXCEPT and the body of the function for clarity
I read about how types are deduced when the template parameter is a pointer(_Ty*
), reference(_Ty
&), universal reference(_Ty
) and where its just the type itself (_Ty
). In this case it is a reference but it has that added removed_reference<_Ty>::type
preceding it; the _Ty
and the reference are split by the call to remove_reference
. How does the compiler figure out the type of _Ty
?
The function template has to figure out the template parameters using the arguments passed in to the function(when not explicitly defining them in the function call) but in this case remove_reference is also a class template that needs to figure out the type of _Ty
as well. It seems almost like a catch 22 for me because std::forward
needs to figure out _Ty
using its function argument but its function argument which is std::remove_reference<_Ty>
needs to already know what _Ty
is. All This tells me is that I have a flawed understanding of how templates work but I don't know where.