template<>
संकलक को बताता है कि एक टेम्पलेट विशेषज्ञता निम्नानुसार है, विशेष रूप से पूर्ण विशेषज्ञता। आम तौर पर, class A
कुछ इस तरह दिखाई करना होगा:
template<class T>
class A{
// general implementation
};
template<>
class A<int>{
// special implementation for ints
};
अब, जब भी A<int>
प्रयोग किया जाता है, विशेष संस्करण का उपयोग किया जाता है। तुम भी उपयोग कर सकते हैं कार्यों विशेषज्ञ:
template<class T>
void foo(T t){
// general
}
template<>
void foo<int>(int i){
// for ints
}
// doesn't actually need the <int>
// as the specialization can be deduced from the parameter type
template<>
void foo(int i){
// also valid
}
आम तौर पर हालांकि, आप simple overloads are generally considered superior के रूप में कार्य के विशेषज्ञ नहीं करना चाहिए,:
void foo(int i){
// better
}
और अब, यह overkill, पीछा कर रहा है बनाने के लिए एक आंशिक विशेषज्ञता:
template<class T1, class T2>
class B{
};
template<class T1>
class B<T1, int>{
};
वो एक पूर्ण विशेषज्ञता के रूप में एक ही तरह से rks, बस जब भी दूसरा टेम्पलेट पैरामीटर int
(उदाहरण के लिए, B<bool,int>
, B<YourType,int>
, आदि) विशेष संस्करण का उपयोग किया जाता है।
ओह ... मैंने इसे "टेम्पलेट <>" के साथ पोस्ट करने से पहले इसे Google पर करने का प्रयास किया जो मुझे अच्छे नतीजे नहीं देता है। सही खोज कुंजी शब्द के लिए धन्यवाद। लेकिन मुझे लगता है कि एसओ के पास इसके अच्छे जवाब हैं। – Vijay