संक्षेप में: क्योंकि टेम्पलेट टेम्पलेट पैरामीटर का उपयोग करना किसी भी फायदे के बिना एक प्रकार पैरामीटर का उपयोग करने से अधिक प्रतिबंधक * है।
* "प्रतिबंधक" से मेरा मतलब है कि आपको "सरल" प्रकार पैरामीटर के साथ एक ही परिणाम प्राप्त करने के लिए एक और जटिल सामग्री की आवश्यकता हो सकती है।
कोई लाभ क्यों नहीं है?
आपका std::stack
शायद इस तरह एक विशेषता है:
template <typename T, typename Container>
struct stack {
Container container;
};
आप Container
की जगह तो एक टेम्पलेट टेम्पलेट पैरामीटर द्वारा, तुम क्यों प्राप्त होगा?
template <typename T, template <typename...> class Container>
struct stack {
Container<T> container;
};
आप T
(Container<T>
) के लिए केवल एक बार और केवल Container
instantiating कर रहे हैं, इसलिए कोई टेम्पलेट टेम्पलेट पैरामीटर के लिए कोई लाभ।
यह अधिक प्रतिबंधक क्यों है?
template <typename T, template <typename....> class Container>
struct stack {
Container<T> container;
};
stack<int, std::vector> // Ok, will use std::vector<int, std::allocator<int>>
लेकिन क्या:
template <typename T, template <typename> class Container>
struct stack;
stack<int, std::vector> // Error: std::vector takes two template arguments
शायद तुम variadic टेम्पलेट का उपयोग कर सकते हैं:
एक टेम्पलेट टेम्पलेट पैरामीटर के साथ
, आप std::stack
करने के लिए एक है जो एक ही हस्ताक्षर बेनकाब जैसे टेम्पलेट, पारित करने के लिए है अगर मैं मानक std::allocator<int>
का उपयोग नहीं करना चाहता हूं?
template <typename T,
template <typename....> class Container = std::vector,
typename Allocator = std::allocator<T>>
struct stack {
Container<T, Allocator> container;
};
stack<int, std::vector, MyAllocator> // Ok...
इसमें कुछ समय गंदा होता जा रहा है ... क्या होगा यदि मैं कि 3/4/एन पैरामीटर लेता है अपने ही कंटेनर टेम्पलेट का उपयोग करना चाहते हैं?
template <typename T,
template <typename... > class Container = std::vector,
typename ...Args>
struct stack {
Container<T, Args...> container;
};
stack<int, MyTemplate, MyParam1, MyParam2> // Ok...
लेकिन, अगर मैं एक गैर-टेम्पलेट वाले कंटेनर का उपयोग करना चाहता हूं तो क्या होगा?
struct foo { };
struct foo_container{ };
stack<foo, foo_container> // Error!
template <typename... >
using foo_container_template = foo_container;
stack<foo, foo_container_template> // Ok...
एक प्रकार पैरामीटर के साथ
ऐसी कोई मुद्दों हैं:
stack<int>
stack<int, std::vector<int, MyAllocator<int>>
stack<int, MyTemplate<int, MyParam1, MyParam2>>
stack<foo, foo_container>
वहाँ अन्य मामलों में इस तरह का एक मिश्रण को स्वीकार टेम्पलेट का उपयोग कर के रूप में जो टेम्पलेट टेम्पलेट पैरामीटर साथ काम नहीं करते हैं विशिष्ट ऑर्डर में प्रकार और गैर-प्रकार पैरामीटर, जिसके लिए आप विविध टेम्पलेट टेम्पलेट पैरामीटर बना सकते हैं, यहां तक कि वैरिएडिक टेम्पलेट्स का उपयोग भी कर सकते हैं।
@CoryKramer यह प्रश्न लिंक किए गए प्रश्न का डुप्लिकेट नहीं है: लिंक किया गया प्रश्न पूछता है कि हमें टेम्पलेट/टाइपनाम को कहां रखना है, जबकि यह प्रश्न इस बात के बारे में है कि मौजूदा वर्ग ('डेक') का एक निश्चित इंटरफ़ेस क्यों है, एक वाक्य रचनात्मक सवाल के बारे में नहीं। – bennofs
डिफ़ॉल्ट टेम्पलेट टेम्पलेट पैरामीटर के लिए भी काम करते हैं? – anatolyg
@anatolyg हां, वे काम करते हैं। –