हां, यह एक गैर-प्रकार का पैरामीटर है। आपके पास कई प्रकार के टेम्पलेट पैरामीटर हो सकते हैं
- पैरामीटर्स टाइप करें।
- प्रकार
- टेम्प्लेट (केवल कक्षाएं और अन्य नाम टेम्पलेट, कोई फ़ंक्शन या चर टेम्पलेट)
- नॉन-टाइप पैरामीटर्स
आपके पास जो है वह अंतिम प्रकार का है। यह एक संकलन समय स्थिर (तथाकथित स्थिर अभिव्यक्ति) है और प्रकार पूर्णांक या गणना है। मानक में इसे देखने के बाद, मुझे कक्षा के खाकों को अनुभागों में ऊपर ले जाना पड़ा - भले ही वे टेम्पलेट प्रकार न हों। लेकिन फिर भी उन प्रकारों का वर्णन करने के उद्देश्य से उन्हें टाइप-पैरामीटर कहा जाता है। आपके पास पॉइंटर्स (और सदस्य पॉइंटर्स) और ऑब्जेक्ट्स / फ़ंक्शंस के संदर्भ भी हो सकते हैं, जिनमें बाहरी लिंकेज हैं (जिन्हें अन्य ऑब्जेक्ट फ़ाइलों से जोड़ा जा सकता है और जिनका पता पूरे प्रोग्राम में अद्वितीय है)। उदाहरण:
टेम्पलेट प्रकार पैरामीटर:
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
};
static constexpr intअपने बजाय टाइप का उपयोग भी कर सकते हैंenum। तोFactorial<0>टेम्पलेट होगाstatic constexpr int value = 1, औरtemplate <int N> struct Factorialहो सकता हैstatic constexpr int value = N * Factorial<N - 1>::value;