हाँ, यह एक गैर प्रकार पैरामीटर है। आपके पास कई प्रकार के टेम्पलेट पैरामीटर
- पैरामीटर्स टाइप कर सकते हैं।
- प्रकार
- टेम्पलेट (केवल वर्गों और उर्फ टेम्पलेट्स, कोई कार्य या चर टेम्पलेट्स)
- गैर प्रकार पैरामीटर
- प्वाइंटर
- संदर्भ
- इंटीग्रल निरंतर भाव
आपके पास आखिरी तरह का क्या है। यह एक संकलित समय निरंतर (तथाकथित निरंतर अभिव्यक्ति) है और प्रकार पूर्णांक या गणना का है। मानक में इसे देखने के बाद, मुझे वर्ग टेम्पलेट को प्रकार अनुभाग में ले जाना पड़ा - भले ही टेम्पलेट्स प्रकार न हों। लेकिन फिर भी उन प्रकारों का वर्णन करने के उद्देश्य से उन्हें टाइप-पैरामीटर कहा जाता है। आपके पास पॉइंटर्स (और सदस्य पॉइंटर्स) और उन ऑब्जेक्ट्स/फ़ंक्शंस के संदर्भ हो सकते हैं जिनमें बाहरी लिंकेज है (जिन्हें अन्य ऑब्जेक्ट फ़ाइलों से जोड़ा जा सकता है और जिसका पता पूरे प्रोग्राम में अद्वितीय है)।उदाहरण:
खाका प्रकार पैरामीटर:
template<typename T>
struct Container {
T t;
};
// pass type "long" as argument.
Container<long> test;
खाका पूर्णांक पैरामीटर:
template<unsigned int S>
struct Vector {
unsigned char bytes[S];
};
// pass 3 as argument.
Vector<3> test;
खाका सूचक पैरामीटर (एक समारोह के लिए एक सूचक गुजर)
template<void (*F)()>
struct FunctionWrapper {
static void call_it() { F(); }
};
// pass address of function do_it as argument.
void do_it() { }
FunctionWrapper<&do_it> test;
खाका संदर्भ पैरामीटर (एक पूर्णांक गुजरना)
template<int &A>
struct SillyExample {
static void do_it() { A = 10; }
};
// pass flag as argument
int flag;
SillyExample<flag> test;
टेम्पलेट टेम्पलेट पैरामीटर।
template<template<typename T> class AllocatePolicy>
struct Pool {
void allocate(size_t n) {
int *p = AllocatePolicy<int>::allocate(n);
}
};
// pass the template "allocator" as argument.
template<typename T>
struct allocator { static T * allocate(size_t n) { return 0; } };
Pool<allocator> test;
किसी भी पैरामीटर के बिना टेम्पलेट संभव नहीं है। लेकिन किसी भी स्पष्ट तर्क के बिना एक टेम्पलेट के लिए संभव है - यह डिफ़ॉल्ट तर्क हैं:
template<unsigned int SIZE = 3>
struct Vector {
unsigned char buffer[SIZE];
};
Vector<> test;
वाक्य रचना, template<>
पैरामीटर के बिना एक टेम्पलेट के बजाय, एक स्पष्ट टेम्पलेट विशेषज्ञता चिह्नित करने के लिए आरक्षित है:
template<>
struct Vector<3> {
// alternative definition for SIZE == 3
};
स्रोत
2009-01-31 16:49:07
आप अपने 'enum' के बजाय' static constexpr int' टाइप भी कर सकते हैं। तो 'फैक्टोरियल <0>' टेम्पलेट में 'स्थैतिक constexpr int value = 1' होगा, और 'टेम्पलेट संरचना फैक्टोरियल' में स्थिर स्थिरांक int मान = एन * फैक्टोरियल :: मान;' –
bobobobo