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
source to share
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 ...
source to share