के साथ विधि पर आकार टेम्पलेट के साथ विधि को प्राथमिकता देते समय, मुझे लगता है कि एकाधिक मिलान उपलब्ध होने पर संकलक सरल मिलान का चयन करेगा।पॉइंटर प्रकार
#include <iostream>
#include <string>
struct A {
static void foo(const char *str) {
std::cout << "1: " << str << std::endl;
}
template<int N> static void foo(const char (&str)[N]) {
std::cout << "2: " << str << std::endl;
}
};
int main()
{
A::foo("hello");
}
उत्पादन 1: hello
है:
इस कोड पर विचार करें। फिर भी, अगर मैं static void foo(const char *str)
विधि पर टिप्पणी करता हूं, तो यह ठीक संकलित करता है और 2: hello
आउटपुट करता है।
मेरे पास कक्षा में दोनों विधियां कैसे हो सकती हैं जैसे ज्ञात आकार वाले सरणी टेम्पलेट विधि को कॉल करेंगे, और पॉइंटर प्रकार गैर-टेम्पलेट विधि को कॉल करेंगे?
मैंने कोशिश की है:
struct A {
template<class _Ty = char>
static void foo(const _Ty *str) {
std::cout << "1: " << str << std::endl;
}
template<int N> static void foo(const char (&str)[N]) {
std::cout << "2: " << str << std::endl;
}
};
लेकिन जी ++ मुझे निम्न त्रुटि देता है:
In function 'int main()':
17:17: error: call of overloaded 'foo(const char [6])' is ambiguous
17:17: note: candidates are:
6:15: note: static void A::foo(const _Ty*) [with _Ty = char]
10:32: note: static void A::foo(const char (&)[N]) [with int N = 6]
सबसे पहले, '_Ty' का उपयोग न करें, जो कार्यान्वयन के लिए आरक्षित है। दूसरा, 'कॉन्स टी * कॉन्स एंड स्ट्र'। –
धन्यवाद, यह काम करता है! टी बनाम _Ty के लिए, मैं सम्मान से असहमत हूँ। 'टी' एक भयानक नामकरण विकल्प है जब इसे अपने टेक्स्ट एडिटर में खोजना पड़ता है। – GaspardP
यह "आदरणीय असहमत" का सवाल नहीं है, यह एक सवाल है "किसी भी स्पष्ट कारण के लिए टूटा जा सकता है।" आप कार्यान्वयन आरक्षित टाइपनाम का उपयोग कर रहे हैं। http://stackoverflow.com/questions/12924243/are-identifiers-starting-with-an-underscore-reserved-according-to-the-latest-c – druckermanly