template<>
class A{
//some class data
};
I have seen this kind of code many times.
what is the use of template<> in the ab
template<> tells the compiler that a template specialization follows, specifically a full specialization. Normally, class A would have to look something like this:
template
class A{
// general implementation
};
template<>
class A{
// special implementation for ints
};
Now, whenever A is used, the specialized version is used. You can also use it to specialize functions:
template
void foo(T t){
// general
}
template<>
void foo(int i){
// for ints
}
// doesn't actually need the
// as the specialization can be deduced from the parameter type
template<>
void foo(int i){
// also valid
}
Normally though, you shouldn't specialize functions, as simple overloads are generally considered superior:
void foo(int i){
// better
}
And now, to make it overkill, the following is a partial specialization:
template
class B{
};
template
class B{
};
Works the same way as a full specialization, just that the specialized version is used whenever the second template parameter is an int (e.g., B, B, etc).