Template (Explicit specialization) in c++ language with hindi explanation , source code

source code , Template (Explicit specialization) in c++ language with hindi explanation :-
इससे पहले के article मे specialization को discuss करेगे जो की function overload या function template के साथ जरुर read करना चाहिए जो की function overload प्रॉब्लम को और अच्छी तरह से समज सकते है |specialzation दो प्रकार का होता है :-

Explicit specialization
जब किसी struture को declare किया जाता है जैसे
struct employee
{
char name [60];
int salary ;
int floor ;
};
और अगर किसी दो struture के दो element मे swap करना होता है तब template को in operation के लिए use किया जा सकता है |
temp = a;
a=b;
b=temp;

इस template से swap operation को perform किया जासकता है | क्योकि c++ language मे struture के elements को swap किया जा सकता है | अगर template a का type employee struture होता है | अतः employee struture के element salary और floor को swap किया जासकता है \ लेकिन name को swap नहीं किया जासकता है |इसके लिए एक  नए swap function की जरुरत होती है | लेकिन swap function मे pass arguments सामान होता है | इसके लिए function template overload को use किया जासकता है | जिसमे function name और pass argument तो सामान है लेकिन उसमे perform किये गये operation अलग होता है |
इस तर ह के function को Explicit specialzation कहते है | जब complier को कोई sepecial definition मिलता है तब complier template को consider नहीं करता है इसके लिए declare किये गये Explicit specialzation function को perform करता है |
Explicit specialzation का form c++ मे time के साथ change होता है लेकिन इस article मे Explicit specialzation का current form को discuss करेगे | जिसे third generation Explicit specialzation कहते है |

Third Generation Explicit specialization
c++ language मे Third Generation Explicit specialzationके निन्म rules को discuss करेगे :-
1.किसी एक function के लिए Third Generation Explicit function , function template और non template function को use किया जा सकता अहि | जिसके लिए function overload होता है |
2.Third Generation Explicit specialzation function की definition और prototype template<> से पहले declare किया जाता है |
3.Third Generation Explicit specialzation function , regular template function को override कर देता है लेकिन regular function , दोनों ही function को override कर देता है |
इसके लिए निन्म उदाहरन होता है :-

#include<iostream.h>
#include<conio.h>
template <class any>
void swap ( any &a, any &b );
struct employee
{
char name [60];
int salary ;
int floor ;
};
template <> void swap <employee > (employee &e1 , employee &e2 );
void show (employee &e);
void main()
{
using namespce std;
int a = 10 ;
int b = 120;
cout<<“Before Swap “;
cout<<“a :”<<a<<“b :”<<b;
cout<<“After swap using integer “;
swap(a,b);
cout<< “Now :”
cout<<“a :”<<a<<“b :”<<b;
employee e1 = {‘Parth’ , 30,000,12};
employee e2 = {‘Meet’ , 20,000,11 };
cout<<“Before Swap “;
show(e1);
show(e2);
cout<<“After swap using Array”;
swap(e1,e2 );
cout<< “Now :”
show(e1);
show(e2);
getch();
}
template< class any >
void swap ( any &a , any &b )
{
any temp ;
temp = a;
a=b;
b =  temp;
}
template <> void swap <employee > (employee &e1 , employee &e2 )
{
double t1 ;
int t2;
t1= e1.salry ;
e1.salary = e2 . salary ;
e2.slary = t1 ;
t2 = e1.floor ;
e1.floor = e2.floor ;
e2.floor = t2 ;
}
void show ( employee e)
{
cout<<“Emplooye name:”<<e.name ;
cout<<“salary :”<<e.salary ;
cout<<“Floor :”<<e.floor;
}

Earlier Approach of specialization
नीचे दिए गये code अभी complier के साथ मे execute नहीं होता है | लेकिन नीचे दिए गये code को भी discuss करना जरुरी है |इसका code होता है :
template <> void swap <employee > (employee &e1 , employee &e2 )
{
double t1 ;
int t2;
t1= e1.salry ;
e1.salary = e2 . salary ;
e2.slary = t1 ;
t2 = e1.floor ;
e1.floor = e2.floor ;
e2.floor = t2 ;
}
को
void swap (employee &e1 , employee  &e2)
replace करते है |

जब complier swap() function को call किया जाता है | तब दो प्रकार के function को generate किया जा सकता है | पहले method मे non template function को generate किया जा सकता है \ और दुसरे method मे template function को use किया जा सकता है | template facility मे non template function को use किया जा सकता है |
इस एप्रोच को बहुत कम ही complier मे use किक्या जा सकता है |
अतः
template <> void swap <employee > (employee &e1 , employee &e2 ) को current form मे use किया जाता है |
void swap<employee>(employee &e1 , employee &e2) को ओल्डर version मे use किया जा सकता है |
ओल्डर version मे template <> को use नहीं किया जा सकता है | लेकिन
नीचे दिए गये
template<> void swap<employee>(employee &e1 , employee &e2)
{
code
}
को
void swap ( employee &e1 , employee &e2 )
{
code UNCHAGED ;
}

इसा article मे, Third Generation Explicit specialzation को discuss किया है अब आगे के article मे template से संबदित कुछ और TERMS को discuss किया जाता है |