static_castを使用してconstexprコンテキストにメンバポインタをアップキャストしてg ++に問題が発生しました。コード例を参照してください。g ++のconstexprコンテキストのメンバポインタのstatic_cast
g ++バージョン6.3および7.0でコンパイルすると、reinterpret_castが定数式ではないというコンパイルエラーが発生します。 clangバージョン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)){}
^~~~~~~~~~~~~~~~~~~~~~~~~~~
これは実際にアップキャストではありません。 –