Template Options Not Used in Partial Specialization

I have the following code:

template<typename T, typename Allocator = std::allocator<T> > class Carray { // ... typedef T* pointer; typedef pointer iterator; // ... }; 

Now I'm trying to do a partial specialization for iterator_traits . It seems good to me, but g ++ 4.4.5 complains:

 #include <iterator> namespace std { template<typename T, typename Allocator> struct iterator_traits<typename Carray<T, Allocator>::iterator> { // line 128 typedef T value_type; typedef typename Allocator::difference_type difference_type; typedef typename Allocator::reference reference; typedef typename Allocator::pointer pointer; typedef typename std::random_access_iterator_tag iterator_category; }; } 

This is the complete error message:

 carray.h:128: error: template parameters not used in partial specialization: carray.h:128: error: 'T' carray.h:130: error: 'Allocator' has not been declared carray.h:131: error: 'Allocator' has not been declared carray.h:132: error: 'Allocator' has not been declared 
+3
source share
1 answer

You don't need specialization here: iterator_traits already specialized for pointer types, and if you end up with an iterator, which is a class type, you can simply define the required typedef in the iterator class.

The problem is that to match the primary specialization, the compiler must accept the arguments that the template is used with, connect them to the specialization, and see if they match.

Consider what happens 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 should the compiler know that T for connecting to S or is it really the value of S<T>::type instead of just int ?

The problem is that the nested typedef ( ::type ) depends on the template parameter ( T ). If this takes place in the argument list of a function or in a partial specialization, the type T cannot be inferred (this is a "non-derivable context").

+9
source

Source: https://habr.com/ru/post/915796/


All Articles