मैं कक्षा के अंदर एक निश्चित रूप से परिभाषित करना चाहता हूं जो अधिकतम संभव int है। कुछ इस तरह:निरंतर अभिव्यक्तियों में numeric_limits :: max() का उपयोग
class A
{
...
static const int ERROR_VALUE = std::numeric_limits<int>::max();
...
}
इस घोषणा निम्न संदेश के साथ संकलित करने के लिए विफल रहता है:
numeric.cpp:8: error: 'std::numeric_limits::max()' cannot appear in a constant-expression numeric.cpp:8: error: a function call cannot appear in a constant-expression
मुझे समझ में क्यों यह काम नहीं करता है, लेकिन दो बातें मुझे अजीब लग रही है:
मुझे लगातार अभिव्यक्तियों में मूल्य का उपयोग करने का एक प्राकृतिक निर्णय लगता है। भाषा डिजाइनर ने अधिकतम() एक फ़ंक्शन को इस उपयोग की अनुमति क्यों नहीं देनी है?
में कल्पना का दावा 18.2.1 कि
For all members declared static const in the numeric_limits template, specializations shall define these values in such a way that they are usable as integral constant expressions.
इसका मतलब यह नहीं है कि मैं और मेरी स्थिति में इसका इस्तेमाल करने में सक्षम होना चाहिए यह त्रुटि संदेश का खंडन नहीं करता है?
धन्यवाद।
आप climits उपयोग कर सकते हैं http://www.cplusplus.com/reference/clibrary/climits/ – Anycorn
@aaa: लेकिन 'climits' टेम्पलेट्स के साथ अच्छा नहीं खेलेंगे। दुर्भाग्य से – UncleBens
@Uncle। सिद्धांत रूप में टेम्पलेट्स में परिभाषित कर सकते हैं, कुछ काम लेकिन करने योग्य। या integer_traits को बढ़ावा दें मुझे लगता है कि उन्हें पहले से ही प्रदान किया गया है: 'integer_traits <> :: const_max/const_min' – Anycorn