पर constexpr संदर्भ में सदस्य सूचक का static_cast मैं static_cast का उपयोग कर एक कॉन्स्टेक्स संदर्भ में सदस्य सूचक के उत्थान के साथ g ++ के साथ एक समस्या में आया। कोड उदाहरण देखें।g ++
जी ++ संस्करण 6.3 और 7.0 के साथ संकलित करते समय वे संकलन त्रुटि देते हैं कि reinterpret_cast निरंतर अभिव्यक्ति नहीं है। जबकि क्लैंग संस्करण 4.0 कोई त्रुटि नहीं देता है, जो मुझे लगता है कि सही है क्योंकि यहां कोई reinterpret_cast नहीं है।
क्या यह g ++ या clang में एक बग है? सही व्यवहार क्या है?
struct Base {};
struct Derived : Base
{
int i;
};
struct Ptr
{
constexpr Ptr(int Derived::* p) : p(static_cast<int Base::*>(p)){}
int Base::* p;
};
constexpr Ptr constexpr_ptr(&Derived::i);
संकलक उत्पादन
g++ -c -std=c++14 test.cpp
test.cpp:17:40: in constexpr expansion of ‘Ptr(&Derived::i)’
test.cpp:11:41: error: a reinterpret_cast is not a constant expression
constexpr Ptr(int Derived::* p) : p(static_cast<int Base::*>(p)){}
^~~~~~~~~~~~~~~~~~~~~~~~~~~
यह वास्तव में एक उथल-पुथल नहीं है। –