Could anyone tell me about template partial specialization..?

B

BekTek

I'm still confused about the template partial specialization which is used
in many libraries..
due to lack of introduction for beginner..

Could you tell me about that in short?
Thanks in advance..
 
S

Sharad Kala

BekTek said:
I'm still confused about the template partial specialization which is used
in many libraries..
due to lack of introduction for beginner..

Consider this code -

#include <iostream>
using namespace std;

template<class T1, class T2>
struct A
{
void foo()
{
cout << "Primary template\n";
}
};

template<class T1>
struct A<T1, int>
{
void foo()
{
cout << "Partial specialization\n";
}
};

int main()
{
A<float, double> a1;
a1.foo(); // Calls primary template

A<float, int> a2;
a2.foo(); // Calls partial specialization
}

Analyze the code, whenever the second template parameter is an int the
partial specialization gets chosen.

Sharad
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top