Specializing a template class to handle its own type

I played around with templates to get a feel for them, and I wanted to specialize the class by type. I've searched the internet for a while, but I haven't found any mention of this.

For example, if I have class Array

:

template<class T>
class Array{
 ...
 void print();
}

      

Can a method be specialized print()

when T=Array<unspecified type>

?

template<class T>
void Array<Array<T>>::print(){
    //do something diffrent for array of array
    //this code wont work
}

      

I managed to do it

template<>
void Array<Array<int>>::print(){
    //print in matrix format
    //this code works
}

      

I don't see this feature being extremely useful, but I was curious

+3


source to share


2 answers


AFAIK you can only do specialization for the whole class. Somehow I needed something like this (in fact I just needed two typedef

to be different), so I created a helper class that only contained the members that were supposed to be specialized and inherited from the main class ...



+2


source


There is a feature called partial specialization where you can apply something like this. However, I do not believe that you can partially specialize member functions without partially specializing the entire class.



+2


source







All Articles