के शरीर के भीतर अनियमित संदर्भ में उपयोग किया जाता है कोड का निम्नलिखित भाग सफलतापूर्वक जीसीसी 5.3.0 के साथ संकलित किया गया है लेकिन क्लैंग 3.7.0 के साथ संकलित करने में विफल रहा है। मैंने ऑनलाइन कॉलिरू कंपाइलर्स का उपयोग दोनों मामलों में एक ही कमांड लाइन विकल्पों के साथ किया था: -std = C++ 14 -O2 -Wall -pedantic -pthread।तर्क केवल एक कॉन्स्टेक्स फ़ंक्शन
#include <cstdio>
// Definition of constexpr function 'foo'.
constexpr std::size_t foo(const int& arg_foo) { return sizeof(arg_foo); }
// Definition of function 'test'.
void test(const int& arg)
{
// The following line produces an error with clang.
constexpr std::size_t res_foo = foo(arg);
// Print the result returned by the 'foo' function.
std::printf("res_foo = %lu\n", res_foo);
}
// Definition of function 'main'.
int main(int argc, const char* argv[])
{
// Test function call.
test(argc);
// Return statement.
return 0;
}
बजना निम्न त्रुटि के साथ खारिज कर दिया:
error: constexpr variable 'res_foo' must be initialized by a constant expression
constexpr size_t res_foo = foo(arg);
~~~~^~~~
दो compilers के बीच इस अंतर की वजह से
, मैं अगर यह कोड का एक मान्य टुकड़ा है सोच रहा हूँ। यदि नहीं, तो मैं इस मामले में क्यों बेहतर समझना चाहता हूं।
जीसीसी बग, कई में से एक जब यह constexpr – TemplateRex
@TemplateRex की बात आती है लेकिन किस तरह से 'foo' किसी कॉन्स्टेक्स फ़ंक्शन के नियमों को तोड़ता है? – Archimaredes
@ आर्किमेरेड्स कोई नहीं, त्रुटि यह है कि 'test' में 'arg'' constexpr' – TemplateRex