अद्यतन: कोड, संकलन चाहिए क्योंकि [class.ctor]/5
पढ़ता है:
The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer (12.6.2) and an empty compound-statement. If that user-written default constructor would satisfy the requirements of a constexpr
constructor (7.1.5), the implicitly-defined default constructor is constexpr
.
और S
के बाद से सिर्फ एक खाली struct है, परोक्ष परिभाषित डिफ़ॉल्ट निर्माता खाली है और इस तरह संतोषजनक constexpr
आवश्यकताओं है।
तो यहां आप कंपाइलर्स की अपूर्णता से निपट रहे हैं, जिसे आपको किसी भी तरह से काम करना है।
पुराना जवाब:
बजना अधिक समझदार त्रुटि संदेश का उत्सर्जन करता है:
main.cpp:3:13: error: default initialization of an object of const type 'const S'
requires a user-provided default constructor
constexpr S s2;
^
[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.(...) [ Example:
struct pixel {
int x, y;
};
constexpr pixel ur = { 1294, 1024 };// OK
constexpr pixel origin; // error: initializer missing
—end example ]
स्रोत
2015-03-27 18:25:23
क्लैंग के लिए, * कॉन्स्ट प्रकार 'कॉन्स्ट एस' के किसी ऑब्जेक्ट का डिफ़ॉल्ट प्रारंभ करने के लिए उपयोगकर्ता द्वारा प्रदान किए गए डिफ़ॉल्ट कन्स्ट्रक्टर * [डेमो] (http://coliru.stacked-crooked.com/a/858633b06d501e42) – Jarod42