Template partial specialization

M

Misiu

Hello,

How to write partial specialization for my template class (see code
bellow) for std::vector<T>::iterator?

Regards,
Misiu

template <typename T>
struct test
{ static const bool value = false; };

template <typename T>
// For vector itself is OK
struct test<std::vector<T> >

//but not for its iterator: expected a type, got ‘std::vector::iterator’
struct test<std::vector<T>::iterator >
{ static const bool value = true; };
 
S

SG

template <typename T>
struct test
{ static const bool value = false; };

template <typename T>
// For vector itself is OK
struct test<std::vector<T> >

//but not for its iterator: expected a type, got ‘std::vector::iterator’
struct test<std::vector<T>::iterator >
{ static const bool value = true; };

Think about it. How can the compiler check whether your type
parameter matches std::vector<T>::iterator for some unknown T? In
case of std::vector this may seem trivial but std::vector is an
ordinary class template which could have looked like this:

template <typename T>
class vector { typedef int iterator; };

If you then explicitly instantiate a class via

template test<int>;

what kind of class should be instantiated? In this case
int==vector<T>::iterator for ALL possible types T. The problem here
is deducing T or in other words inverting the meta function that maps
type T -> type std::vector<T>::iterator. Also, this wouldn't be
really a specialization since std::vector<T>::iterator could be
basically anything you want it to be via specialization of
std::vector.

Cheers!
SG
 
S

SG

Hello,

How to write partial specialization for my template class (see code
bellow) for std::vector<T>::iterator?

Regards,
Misiu

You may want to rethink your design.

Anyhow, with a bit of meta-programming you could achieve a similar
effect.

template<bool B, typename T1, typename T2>
struct if_ { typedef T1 type; };
template<typename T1, typename T2>
struct if_<false,T1,T2> { typedef T2 type; };

template<typename T1, typename T2>
struct same_ { static const bool value = false; };
template<typename T>
struct same_<T,T> { static const bool value = true; };

template<typename T>
class impl_default {
// ...
protected:
~default_impl() {}
};

template<typename T>
class impl_for_vect_iter {
// ...
protected:
~spec_for_vect_iter() {}
};

template<typename T>
class test : public if_<
same_<T,typename std::vector<T>::iterator>::value,
impl_for_vect_iter said:

or something like this. (I didn't test it)

Cheers!
SG
 
S

SG

(I didn't test it)

which explains the error:
  template<typename T>
  class test : public if_<
    same_<T,typename std::vector<T>::iterator>::value,

The parameters to this meta function "same_" won't work like
intended. You would need some other meta function which tests whether
T is an iterator and if yes deduces the associated value_type
(possibly via std::iterator_traits) and then checks whether
std::vector<value_type>::iterator==T.

But I think this is really overkill. You might be better served with
another design approach. It might be a good idea to restrict the use
of specializations (i.e. to traits classes).

Cheers!
SG
 
M

Misiu

Think about it. How can the compiler check whether your type
parameter matches std::vector<T>::iterator for some unknown T? In
case of std::vector this may seem trivial but std::vector is an
ordinary class template which could have looked like this:

template <typename T>
class vector { typedef int iterator; };

If you then explicitly instantiate a class via

template test<int>;

what kind of class should be instantiated? In this case
int==vector<T>::iterator for ALL possible types T. The problem here
is deducing T or in other words inverting the meta function that maps
type T -> type std::vector<T>::iterator. Also, this wouldn't be
really a specialization since std::vector<T>::iterator could be
basically anything you want it to be via specialization of
std::vector.

Thanks for explanantion.
Regards,
Misiu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top