How to remove_reference disable template argument output?

According to this link, the template argument argument, which is forbidden for std::forward and std::remove_reference , helps us achieve this. But how remove_reference using remove_reference prevent template output here?

 template <class S> S&& forward(typename std::remove_reference<S>::type& t) noexcept { return static_cast<S&&>(t); } 
+6
source share
2 answers

S in the expression typename std::remove_reference<S>::type represents an inaccessible context (especially because S appears in the nested specifier of the type specified using an identifier with qualification). Non-derivable contexts, as the name implies, are contexts in which the template argument cannot be inferred.

This case provides a simple example to understand why. Say I had:

 int i; forward(i); 

What would be S ? It can be int , int& or int&& - all these types will lead to the correct argument type for the function. It is simply not possible for the compiler to determine which S you really mean here, so it is not trying. It is not output, so you must explicitly indicate which S you mean:

 forward<int&>(i); // oh, got it, you meant S=int& 
+6
source

This is how the remove_reference function is implemented:

 template< class T > struct remove_reference {typedef T type;}; template< class T > struct remove_reference<T&> {typedef T type;}; template< class T > struct remove_reference<T&&> {typedef T type;}; 
-4
source

All Articles