इस लेख में हम C++ STL में std::is_fundamental टेम्पलेट के कार्य, सिंटैक्स और उदाहरणों पर चर्चा करेंगे।
is_fundamental एक टेम्पलेट है जो <type_traits> हेडर फाइल के अंतर्गत आता है। यह टेम्पलेट जांचता है कि दिया गया टाइप T एक फंडामेंटल डेटा टाइप है या नहीं।
फंडामेंटल टाइप क्या है?
फंडामेंटल टाइप्स बिल्ट-इन टाइप्स हैं जो कंपाइलर में पहले से घोषित होते हैं। जैसे int, float, char, double, आदि। इन्हें बिल्ट-इन डेटा टाइप्स के रूप में भी जाना जाता है।
वे सभी डेटा टाइप्स जो यूजर-डिफाइंड हैं जैसे: क्लास, एनम, स्ट्रक्चर, रेफरेंस या पॉइंटर्स, फंडामेंटल टाइप का हिस्सा नहीं हैं।
सिंटैक्स
template <class T> is_fundamental;
पैरामीटर्स
टेम्पलेट में केवल टाइप T का एक पैरामीटर हो सकता है, और यह जांचता है कि दिया गया टाइप फाइनल क्लास टाइप है या नहीं।
रिटर्न वैल्यू
यह एक बूलियन मान लौटाता है, यदि दिया गया टाइप फंडामेंटल डेटा टाइप है तो true, और यदि दिया गया टाइप फंडामेंटल डेटा टाइप नहीं है तो false।
उदाहरण
Input: class final_abc; is_fundamental<final_abc>::value; Output: False Input: is_fundamental<int>::value; Output: True Input: is_fundamental<int*>::value; Output: False
उदाहरण 1
#include <iostream>
#include <type_traits>
using namespace std;
class TP {
// TP Body
};
int main() {
cout << boolalpha;
cout << "checking for is_fundamental:";
cout << "\nTP: " << is_fundamental<TP>::value;
cout << "\nchar : " << is_fundamental<char>::value;
cout << "\nchar& : " << is_fundamental<char&>::value;
cout << "\nchar* : " << is_fundamental<char*>::value;
return 0;
}
आउटपुट
checking for is_fundamental: TP: false char : true char& : false char* : false
उदाहरण 2
#include <iostream>
#include <type_traits>
using namespace std;
int main() {
cout << boolalpha;
cout << "checking for is_fundamental:";
cout << "\nint: " << is_fundamental<int>::value;
cout << "\ndouble : " << is_fundamental<double>::value;
cout << "\nint& : " << is_fundamental<int&>::value;
cout << "\nint* : " << is_fundamental<int*>::value;
cout << "\ndouble& : " << is_fundamental<double&>::value;
cout << "\ndouble* : " << is_fundamental<double*>::value;
return 0;
}
आउटपुट
checking for is_fundamental: int: true double : true int& : false int* : false double& : false double* : false