Template parameters not used in partial specialization

后端 未结 1 1654
北海茫月
北海茫月 2020-12-11 21:57

I have the following code:

template >
class Carray {
    // ...
    typedef T* pointer;
    ty         


        
相关标签:
1条回答
  • 2020-12-11 22:07

    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").

    0 讨论(0)
提交回复
热议问题