इस लेख में हम C++ STL में std::is_const टेम्पलेट के कार्य, सिंटैक्स और उदाहरणों पर चर्चा करेंगे।
C++ में is_const टेम्पलेट का उपयोग यह जांचने के लिए किया जाता है कि परिभाषित टाइप const-क्वालिफाइड टाइप है या नहीं।
const-क्वालिफाइड टाइप क्या है?
हम एक टाइप को const-क्वालिफाइड कहते हैं जब टाइप का मान स्थिर होता है। कॉन्स्टेंट डेटा टाइप एक ऐसा टाइप है जिसमें एक बार const में मान इनिशियलाइज़ होने के बाद पूरे प्रोग्राम में बदला या परिवर्तित नहीं किया जा सकता।
सिंटैक्स
template <class T> is_const;
पैरामीटर्स
टेम्पलेट में केवल टाइप T का एक पैरामीटर हो सकता है, और यह जांचता है कि दिया गया टाइप const-क्वालिफायर है या नहीं।
रिटर्न वैल्यू
यह एक बूलियन मान लौटाता है, यदि दिया गया टाइप const-क्वालिफायर है तो true, और यदि दिया गया टाइप const-क्वालिफायर नहीं है तो false।
उदाहरण
Input: is_const<const int>::value; Output: True Input: is_const<int>::value; Output: False
उदाहरण 1
#include <iostream>
#include <type_traits>
using namespace std;
int main() {
cout << boolalpha;
cout << "checking for is_const template: ";
cout << "\nInt : " << is_const<int>::value;
cout << "\nConst int : " << is_const<const int>::value;
cout << "\nConst int& : " << is_const<const int&>::value;
return 0;
}
आउटपुट
checking for is_const template: Int : false Const int : true Const int& : false
उदाहरण 2
#include <iostream>
#include <type_traits>
using namespace std;
int main() {
cout << boolalpha;
cout << "checking for is_const template: ";
cout << "\nFloat : " << is_const<float>::value;
cout << "\nChar : " << is_const<char>::value;
cout << "\nFloat *: " << is_const<float*>::value;
cout << "\nChar *: " << is_const<char*>::value;
cout << "\nConst int* : " << is_const<const int*>::value;
cout << "\nint* const : " << is_const<int* const>::value;
return 0;
}
आउटपुट
checking for is_const template: Float : false Char: false Float *: false Char *: false Const int* : false int* const: true