पैरामीटरेटेड प्रकार जैसे सी ++ टेम्पलेट्स एक अच्छी बात है, लेकिन अधिकांश समय वे केवल अन्य प्रकारों द्वारा parametrized किया जा सकता है।क्या कोई प्रोग्रामिंग भाषा है जहां मूल्यों को पैरामीटर द्वारा पैरामीटर किया जा सकता है?
हालांकि, सी ++ में एक विशेष मामला है जहां एक पूर्णांक द्वारा टेम्पलेट को पैरामीट्रिज करना संभव है। उदाहरण के लिए, निश्चित लंबाई सरणियों एक विशिष्ट उपयोग के मामले हैं:
template<typename T, int SIZE> class FixedArray
{
T m_values[SIZE];
public:
int getElementCount() const { return SIZE; }
T operator[] (int i) const {
if (i<0 || i>=SIZE)
throw;
else
return m_values[i];
}
};
void f()
{
FixedArray<float, 5> float_array; // Declares a fixed array of 5 floats.
//The size is known at compile time, and values are allocated on the stack.
}
केवल निरंतर पूर्णांकों और संकेत C++ अनुमति है, लेकिन मुझे लगता है कि यह parametrization के लिए किसी भी मूल्य का उपयोग करने के लिए दिलचस्प हो सकता है (तैरता है, वर्ग उदाहरणों, आदि ।)। यह संकलन-समय पर आमतौर पर पूर्व शर्त को व्यक्त करने की अनुमति दे सकता है (आमतौर पर दस्तावेज़ीकरण में अनौपचारिक रूप से निर्दिष्ट), और रनटाइम पर उन्हें स्वचालित रूप से जांचें।
// Interval of type T between TMin and TMax.
template<typename T, T TMin, T TMax> class Interval
{
T m_value;
public:
Interval(int val) { *this = val; }
Interval& operator = (T val) {
//check that 'val is in [TMin, TMax] and set the value if so, throw error if not
if (val < TMin || val > TMax)
throw;
else
m_value = val;
return *this;
};
operator T() const { return m_value; }
}
// Now define a f function which is only allowing a float between O and 1
// Usually, that kind of function is taking a float parameter with the doc saying "the float is in 0-1 range". But with our Interval template we have
// that constraint expressed in the type directly.
float f(Interval<float, 0, 1> bounded_value)
{
// No need to check for boundaries here, since the template asserts at compile-time that the interval is valid
return ...;
}
// Example usage
void main();
{
float val = 0.5;
Interval<float, 0, 1> sample_interval = val; // OK. The boundary check is done once at runtime.
f(sample_interval); // OK. No check, because it is asserted by the type constraint at compile-time.
// This can prevent endless precondition testing within an algorithm always using the same interval
sample_interval = 3*val; // Exception at runtime because the constraint is no longer satisfied
f(sample_interval); // If I am here, I am sure that the interval is valid. But it is not the case in that example.
}
क्या तब दिलचस्प हो सकता है इन प्रकार के बीच संबंधों को व्यक्त करने के लिए किया जाएगा: उदाहरण के लिए, यहाँ एक काल्पनिक सी ++ बोली में एक "अंतराल" टेम्पलेट है। उदाहरण के लिए, किसी अन्य अंतराल बी के साथ अंतराल ए को अन्य सीमाओं के साथ असाइन करने के लिए नियम व्यक्त करना, या संकलन-समय पर जांच की गई सभी चीज़ों के साथ अंतराल को मान असाइन करने का नियम।
क्या इस तरह के पैरामीट्रिजेशन (या एक समान दृष्टिकोण) के साथ कोई भाषा है, या किसी का अभी भी आविष्कार किया जाना चाहिए? कोई उपयोगी शोध पत्र?
पास्कल में अभिन्न प्रकार के उपन्यास हैं जिन्हें इस विशेष मामले के रूप में देखा जा सकता है। – Joey