का हवाला देते हुए N4140 [dcl.constexpr]/9:
A constexpr
specifier used in an object declaration declares the object as const
. Such an object shall have literal type and shall be initialized.
शाब्दिक प्रकार/10 में [basic.types] परिभाषित किया गया है:
A type is a literal type if it is:
(10.1) — void
; or
(10.2) — a scalar type; or
(10.3) — a reference type; or
(10.4) — an array of literal type; or
(10.5) — a class type (Clause 9) that has all of the following properties:
(10.5.1) — it has a trivial destructor,
(10.5.2) — it is an aggregate type (8.5.1) or has at least one constexpr
constructor or constructor template that is not a copy or move constructor, and
(10.5.3) — all of its non-static data members and base classes are of non-volatile literal types.
अदिश प्रकार पैरा 9 में है:
Arithmetic types (3.9.1), enumeration types, pointer types, pointer to member types (3.9.2), std::nullptr_t
, and cv-qualified versions of these types (3.9.3) are collectively called scalar types.
int
अंकगणितीय है, इसलिए volatile int
एक स्केलर प्रकार है और इसलिए एक शाब्दिक प्रकार है। constexpr volatile int i = 5;
इस प्रकार एक अच्छी तरह से घोषित घोषणा है।
दिलचस्प है, एक अभिव्यक्ति है कि मूल्यांकन करता i
एक कोर-निरंतर अभिव्यक्ति नहीं किया जा सकता क्योंकि यह अस्थिर प्रकार का एक glvalue ([expr.const]/2) के लिए एक lvalue करने वाली rvalue रूपांतरण लागू होता है। नतीजतन, i
का मूल्यांकन करने वाले अभिव्यक्ति न तो अभिन्न स्थिर अभिव्यक्ति और न ही निरंतर अभिव्यक्ति हैं। मुझे यकीन नहीं है कि constexpr
उस घोषणा में i
को const
, और (@T.C. पर ध्यान दें) को इसके प्रारंभिक अभिव्यक्ति के लिए निरंतर अभिव्यक्ति होने की आवश्यकता है।
मैंने इसे GCC bug 65327 के रूप में रिपोर्ट किया है, हम देखेंगे कि जीसीसी लोगों को क्या कहना है।
2015-03-16 अपडेट: बग जीसीसी के लिए तय किया गया है 5.
क्या है यह आप को व्यक्त करने की कोशिश कर रहे हैं? वे अनिवार्य रूप से विरोध कर रहे हैं। इसे सेट करने के बाद निरंतर कभी भी नहीं बदलेगा, लेकिन एक अस्थिरता सभी को बदलने की गारंटी है (संभवतया किसी अन्य धागे से भी)। – TheBuzzSaw
'constexpr' और' const' समान नहीं हैं –
@TheBuzzSaw: 'अस्थिर 'का अर्थ यह नहीं है कि कुछ भी बदलने की प्रवृत्ति है। इसका मतलब यह है कि स्मृति पहुंच के दुष्प्रभाव हो सकते हैं, और इसलिए संकलक को I/O के रूप में इसका इलाज करना चाहिए। जब भी स्थिर रहने के लिए मूल्य की गारंटी दी जाती है तब भी सिस्टम पर 'अस्थिर' का उपयोग करना आवश्यक हो सकता है। – Mehrdad