मैंने कोड लिखने के लिए http://en.cppreference.com/w/cpp/language/typeid को संदर्भित किया है जो विभिन्न प्रकार के लिए अलग-अलग चीजें करता है।तर्क में चर बनाम स्ट्रिंग के लिए std :: स्ट्रिंग का आईडी टाइप करें?
कोड नीचे जैसा है और टिप्पणियों में स्पष्टीकरण दिया गया है।
#include <iostream>
#include <typeinfo>
using namespace std;
template <typename T>
void test_template(const T &t)
{
if (typeid(t) == typeid(double))
cout <<"double\n";
if (typeid(t) == typeid(string))
cout <<"string\n";
if (typeid(t) == typeid(int))
cout <<"int\n";
}
int main()
{
auto a = -1;
string str = "ok";
test_template(a); // Prints int
test_template("Helloworld"); // Does not print string
test_template(str); // Prints string
test_template(10.00); // Prints double
return 0;
}
क्यों नहीं करता करता test_template(str)
प्रिंट जबकि test_template("Helloworld")
"स्ट्रिंग"?
BTW, मेरी जी ++ संस्करण ग्राम (उबंटू 5.4.0-6ubuntu1 ~ 16.04.4) 5.4.0 "Helloworld"
तरह 20160609.
डाउनवॉट्स क्यों? बेशक, यह एक बुनियादी स्तर पर है, लेकिन सवाल पूर्ण, स्पष्ट है, और ओपी में एक कंपाइलर संस्करण भी शामिल है! – TartanLlama
'" Helloworld "' 'std :: string' नहीं है। लेकिन '" होलोर्ल्ड "एस होगा। – Jarod42
@TartanLlama: मुझे लगता है कि यह एक उपयोगकर्ता की वजह से है जो बहुत ही नाजुक कोड लिखने के लिए 'टाइपिड' का उपयोग करना चाहता है। –