Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C++

C++ में is_standard_layout टेम्पलेट

इस लेख में हम C++ STL में std::is_standard_layout टेम्पलेट के कार्य, सिंटैक्स और उदाहरणों पर चर्चा करेंगे।

is_standard_layout एक टेम्पलेट है जो <type_traits> हेडर फाइल के अंतर्गत आता है। यह टेम्पलेट जांचता है कि दिया गया टाइप T एक स्टैंडर्ड लेआउट है या नहीं।

C++ में स्टैंडर्ड लेआउट क्या है?

स्टैंडर्ड लेआउट टाइप एक ऐसा टाइप है जो सिंपल लीनियर स्ट्रक्चर (जैसे हम एरे में देखते हैं) और एक्सेस कंट्रोल का होता है जिसे अन्य प्रोग्रामिंग भाषाओं में लिखे गए प्रोग्राम के साथ संचार करने के लिए आसानी से उपयोग किया जा सकता है। यह एक ऐसा टाइप है जिसे सभी प्रोग्रामिंग भाषाओं के लिए एक कोड के विचार के साथ लिखा जाता है।

एक स्टैंडर्ड लेआउट क्लास में निम्नलिखित विशेषताएं होती हैं -

  • इसमें कोई वर्चुअल फंक्शन या वर्चुअल क्लास नहीं होनी चाहिए।
  • सभी क्लासेस के लिए समान एक्सेस कंट्रोल (पब्लिक, प्राइवेट, प्रोटेक्टेड) होना चाहिए।
  • कोई नॉन-स्टेटिक डेटा टाइप नहीं होने चाहिए।
  • इसके पहले नॉन-स्टेटिक डेटा टाइप के समान टाइप की कोई बेस क्लास नहीं होनी चाहिए।

सिंटैक्स

template <class T> is_standard_layout;

पैरामीटर्स

टेम्पलेट में केवल टाइप T का एक पैरामीटर हो सकता है, और यह जांचता है कि T स्टैंडर्ड लेआउट टाइप है या नहीं।

रिटर्न वैल्यू

यह एक बूलियन मान लौटाता है, यदि दिया गया टाइप स्टैंडर्ड लेआउट टाइप है तो true, और यदि दिया गया टाइप स्टैंडर्ड लेआउट टाइप नहीं है तो false।

उदाहरण

Input: is_standard_layout<int>::value; Output: True
Input: class A{ int a; }; class B : public A { int b; }; is_standard_layout<B>::value; Output: False

उदाहरण 1

#include <iostream>
#include <type_traits>
using namespace std;

class TP {
    int var;
};
struct TP_2 {
    int var;
    private:
    int var_2;
};
union TP_3 { };

int main() {
    cout << boolalpha;
    cout << "checking for is_standard_layout template for class : " << is_standard_layout<TP>::value;
    cout << "\nchecking for is_standard_layout template for structure: " << is_standard_layout<TP_2>::value;
    cout << "\nchecking for is_standard_layout template for union : " << is_standard_layout<TP_3>::value;
    cout << "\nchecking for is_standard_layout template for char : " << is_standard_layout<char>::value;
    cout << "\nchecking for is_standard_layout template for int : " << is_standard_layout<int>::value;
    return 0;
}

आउटपुट

checking for is_standard_layout template for class : true
checking for is_standard_layout template for structure: false
checking for is_standard_layout template for union : true
checking for is_standard_layout template for char : true
checking for is_standard_layout template for int : true

उदाहरण 2

#include <iostream>
#include <type_traits>
using namespace std;

class TP {
    int var;
};
class TP_2 {
    int var;
    private:
    int var_2;
};
class TP_3 { };

int main() {
    cout << boolalpha;
    cout << "checking for is_standard_layout template for class with one variable : " << is_standard_layout<TP>::value;
    cout << "\nchecking for is_standard_layout template for class with one private variable: " << is_standard_layout<TP_2>::value;
    cout << "\nchecking for is_standard_layout template for class with no variable : " << is_standard_layout<TP_3>::value;
    cout << "\nchecking for is_standard_layout template for int * : " << is_standard_layout<int*>::value;
    cout << "\nchecking for is_standard_layout template for float : " << is_standard_layout<float>::value;
    return 0;
}

आउटपुट

checking for is_standard_layout template for class with one variable : true
checking for is_standard_layout template for class with one private variable: true
checking for is_standard_layout template for class with no variable : true
checking for is_standard_layout template for int * : true
checking for is_standard_layout template for float : true
  1. - C++ में टेम्पलेट स्पेशलाइजेशन

    इस ट्यूटोरियल में, हम C++ में टेम्पलेट स्पेशलाइजेशन (Template Specialization) को समझने के लिए एक प्रोग्राम पर चर्चा करेंगे। sort() जैसे मानक फंक्शन्स किसी भी डेटा टाइप के साथ उपयोग किए जा सकते हैं और वे प्रत्येक के साथ समान व्यवहार करते हैं। लेकिन यदि आप किसी विशेष डेटा टाइप (यूजर-डिफाइंड सहित) के

  1. - C++ में टेम्पलेट स्पेशलाइज़ेशन

    C++ में, टेम्पलेट्स का उपयोग सामान्यीकृत फ़ंक्शन्स और क्लासेज बनाने के लिए किया जाता है। इसलिए हम int, char, float, या कुछ उपयोगकर्ता-परिभाषित डेटा जैसे किसी भी प्रकार के डेटा का उपयोग टेम्पलेट्स के साथ कर सकते हैं। इस खंड में, हम देखेंगे कि टेम्पलेट स्पेशलाइज़ेशन का उपयोग कैसे करें। अब हम विभिन्न

  1. - C++ में टेम्पलेट मेटाप्रोग्रामिंग

    जब हम टेम्पलेट का उपयोग करके कंपाइल टाइम पर गणना करने के लिए प्रोग्राम लिखते हैं, तो इसे टेम्पलेट मेटाप्रोग्रामिंग कहा जाता है।उदाहरण कोड#include <iostream> using namespace std; template<int n>struct power {    enum { value = 4*power<n-1>::value }; }; template<>stru