2011-06-06 9 views
6

मेरे पास फ़ंक्शन का एक समूह है जो कोड की एक पंक्ति को छोड़कर पूरी तरह से समान पढ़ता है, जो इनपुट पैरामीटर के प्रकार के आधार पर अलग है।कोड के लिए टेम्पलेट जो समान है लेकिन समान नहीं है?

उदाहरण:

void Func(std::vector<int> input) 
{ 
    DoSomethingGeneral1(); 
    ... 
    DoSomethingSpecialWithStdVector(input); 
    ... 
    DoSomethingGeneral2(); 
} 

void Func(int input) 
{ 
    DoSomethingGeneral1(); 
    ... 
    DoSomethingSpecialWithInt(input); 
    ... 
    DoSomethingGeneral2(); 
} 

void Func(std::string input) 
{ 
    DoSomethingGeneral1(); 
    ... 
    DoSomethingSpecialWithStdString(input); 
    ... 
    DoSomethingGeneral2(); 
} 

मुझे आश्चर्य है कि कैसे मैं एक टेम्पलेट की तरह तंत्र का उपयोग करते हुए इस दोहराव से बचने के सकता है। अगर मैं सही ढंग से "विशेषज्ञता" को समझता हूं, तो यह विशेष कार्यों के कोड को दो बार से बचने से नहीं बचाता है?

+1

हम एक साहसिक कदम नहीं ले सकते जब तक यह जान न जाए कि DoSomethingSpecial क्या वास्तव में करना है। क्या आप अधिक जानकारी दे सकते हैं? यदि टेम्पलेट के लिए वास्तव में उपयुक्त नहीं है, तो इसे सामान्य इंटरफेस के अंदर बनाने के लिए हमें कुछ काम करना होगा और कॉलर के परिप्रेक्ष्य से इसे ट्विक करना होगा। – sarat

उत्तर

8

आप जाते हैं .. पैरामीटर को संदर्भित करने के लिए बदल दिया ences से बचने के लिए प्रतियां + आश्वासन आप समारोह() में फिर से बदल मूल्यों का उपयोग कर सकते

void DoSomethingSpecial(std::vector<int>& input){} 

void DoSomethingSpecial(int& input){} 

void DoSomethingSpecial(std::string& input){} 

template< typename T > 
void Func(T input) 
{ 
    DoSomethingGeneral1(); 
    DoSomethingSpecial(input); 
    DoSomethingGeneral2(); 
} 
1

एक जेनेरिक वर्जन घोषित लेकिन केवल विशेषज्ञताओं को परिभाषित:

template<class T> 
void DoSpecificStuff(T& withWhat); 

template<> 
void DoSpecificStuff(int& withWhat) 
{ 
    //implementation 
} 

template<> 
void DoSpecificStuff(std::vector<int>& withWhat) 
{ 
    //implementation 
} 
3

सबसे अच्छा तरीका है Func टेम्पलेट के रूप में बनाने के लिए है:

template<typename T> 
void Func(T input); 

और DoSomethingSpecial...() ओवरलोड: यहाँ

void DoSomethingSpecial(std::string); 
void DoSomethingSpecial(int); 
void DoSomethingSpecial(std::vector<int>); 
4

मेरा मानना ​​है कि टेम्पलेट speciaization के लिए कोई जरूरत नहीं है, आप सरल समारोह से अधिक भार, कुछ इस तरह उपयोग कर सकते हैं:

void DoSomethingSpecial(const std::vector<int>& input) 
{ 
} 

void DoSomethingSpecial(string s) 
{ 
} 

void DoSomethingSpecial(int input) 
{ 
} 
template <typename T> 
void Func(const T& input) 
{ 
    //DoSomethingGeneral1(); 
    DoSomethingSpecial(input); 
    //DoSomethingGeneral2(); 
} 

int main() 
{ 
    std::vector<int> a; 
    Func(a); 
    Func("abc"); 
    Func(10); 
} 
+0

धन्यवाद - मैं बस विश्वास नहीं कर सका यह काम करेगा, लेकिन यह करता है! :) –

3

शैली के मामले में, आप उन हिस्सों को निकालना चाहते हैं जो एक छोटे से टेम्पलेट वर्ग के रूप में अलग हैं जो अधिक आसानी से विशिष्ट है। इस तकनीक के उदाहरण के रूप में, एक अनॉर्डर्ड सेट (या हैश_सेट) के अपने पसंदीदा टेम्पलेट कार्यान्वयन पर विचार करें। इस तरह के कार्यान्वयन के लिए आपको एक सरल हैश_की < टी> टेम्पलेट का विशेषज्ञ होना चाहिए यदि कोई विशेषज्ञता पहले से उपलब्ध नहीं है। उन्हें आपको पूर्ण कंटेनर का विशेषज्ञ बनाने की आवश्यकता नहीं है।

हालांकि अपने उदाहरण काफी सरल बस पूरे समारोह विशेषज्ञ, सामान्य रूप में मैं समारोह < टी लागू करेगा> सामान्य रूप से और विशेषज्ञ DoSomethingSpecial < टी> इसलिए की तरह है:

template< class T > 
void DoSomethingSpecial(T &input) 
{ 
... 
} 

template< class T > 
void Func(T input) 
{ 
DoSomethingGeneral1(); 
... 
DoSomethingSpecial(T); 
... 
DoSomethingGeneral2(); 
} 

template<> 
void DoSomethingSpecial(std::string &input) 
{ 
... 
} 

template<> 
void DoSomethingSpecial(int &input) 
{ 
... 
} 
+0

मैं इस पर विचार करूंगा, धन्यवाद। –

संबंधित मुद्दे