Template parameters not used in partial specialization

坚强是说给别人听的谎言 提交于 2019-11-28 13:55:15

You shouldn't need a specialization at all here: iterator_traits is already specialized for pointer types and if you do end up with an iterator that is a class type, you can just define those required typedefs in the iterator class.

The problem is that in order to match the primary specialization, the compiler needs to take the arguments with which the template is used, plug them into the specialization, and see whether they match.

Consider what would happen in the following simplified scenario:

template <typename T> struct S { typedef int type; };

template <typename T> 
struct Traits { };

template <typename T> 
struct Traits<typename S<T>::type> { };

How is the compiler supposed to know what T to plug into S or whether some S<T>::type is really meant instead of just int?

The problem is that the nested typedef (::type) depends on the template parameter (T). When this is the case in a function argument list or in a partial specialization, the type T cannot be deduced (it is a "non-deduced context").

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!