11
我遇到了一個g ++問題,使用static_cast在constexpr上下文中上傳了一個成員指針。看代碼示例。static_cast成員指針在g ++上的constexpr上下文中
當使用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)){}
^~~~~~~~~~~~~~~~~~~~~~~~~~~
這不是一個真正的upcast。 –