इस लेख में हम C++ STL में std::is_arithmetic टेम्पलेट के कार्य, सिंटैक्स और उदाहरणों पर चर्चा करेंगे।
is_arithmetic टेम्पलेट यह जांचने में मदद करता है कि दिया गया क्लास T अंकगणितीय टाइप का है या नहीं।
अंकगणितीय टाइप क्या है?
अंकगणितीय टाइप में दो टाइप होते हैं, जो हैं:
इंटीग्रल टाइप्स - इसमें हम पूर्ण संख्याएं परिभाषित करते हैं। इंटीग्रल टाइप्स के निम्नलिखित प्रकार हैं -
- char
- bool
- int
- long
- short
- long long
- wchar_t
- char16_t
- char32_t
फ्लोटिंग पॉइंट टाइप्स - ये भिन्नात्मक भाग रख सकते हैं। फ्लोटिंग पॉइंट के निम्नलिखित प्रकार हैं।
- Float
- Double
- Long double
तो, टेम्पलेट is_arithmetic परिभाषित टाइप T की जांच करता है कि यह अंकगणितीय टाइप है या नहीं और तदनुसार true या false लौटाता है।
सिंटैक्स
template <class T> is_arithmetic;
पैरामीटर्स
एक टेम्पलेट में केवल एक पैरामीटर हो सकता है जो टाइप T का है, और पैरामीटर की जांच करता है कि यह अंकगणितीय टाइप का है या नहीं।
रिटर्न वैल्यू
यह फंक्शन एक बूल टाइप मान लौटाता है, जो true या false हो सकता है। यह true लौटाता है यदि दिया गया टाइप अंकगणितीय है और false यदि टाइप अंकगणितीय नहीं है।
उदाहरण
Input: is_arithmetic<bool>::value; Output: True Input: is_arithmetic<class_a>::value; Output: false
उदाहरण 1
#include <iostream>
#include <type_traits>
using namespace std;
class TP { };
int main() {
cout << boolalpha;
cout << "checking for is_arithmetic template:";
cout << "\nTP class : " << is_arithmetic<TP>::value;
cout << "\n For Bool value: " << is_arithmetic<bool>::value;
cout << "\n For long value : " << is_arithmetic<long>::value;
cout << "\n For Short value : " << is_arithmetic<short>::value;
return 0;
}
आउटपुट
checking for is_arithmetic template: TP class : false For Bool value: true For long value : true For Short value : true
उदाहरण 2
#include <iostream>
#include <type_traits>
using namespace std;
int main() {
cout << boolalpha;
cout << "checking for is_arithmetic template:";
cout << "\nInt : " << is_arithmetic<int>::value;
cout << "\nchar : " << is_arithmetic<char>::value;
cout << "\nFloat : " << is_arithmetic<float>::value;
cout << "\nDouble : " << is_arithmetic<double>::value;
cout << "\nInt *: " << is_arithmetic<int*>::value;
cout << "\nchar *: " << is_arithmetic<char*>::value;
cout << "\nFloat *: " << is_arithmetic<float*>::value;
cout << "\nDouble *: " << is_arithmetic<double*>::value;
return 0;
}
आउटपुट
checking for is_arithmetic template: Int : true Char : true Float : true Double : true Int * : false Char *: false Float *: false Double *: false