template<>
class A{
//some class data
};
I have seen this kind of code many times.
what is the use of template<> in the above code?
And what are the cases where we need mandate the use of it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
template<>tells the compiler that a template specialization follows, specifically a full specialization. Normally,class Awould have to look something like this:Now, whenever
A<int>is used, the specialized version is used. You can also use it to specialize functions:Normally though, you shouldn’t specialize functions, as simple overloads are generally considered superior:
And now, to make it overkill, the following is a partial specialization:
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<bool,int>,B<YourType,int>, etc).