GCC 8.2.1 and MSVC 19.20 compile the below code but Clang 8.0.0 and ICC 19.0.1 fail to do so.
// Base class.
struct Base {};
// Data class.
struct Data { int foo; };
// Derived class.
struct Derived : Base, Data { int bar; };
// Main function.
int main()
{
constexpr int Data::* data_p{ &Data::foo };
constexpr int Derived::* derived_p{ data_p };
constexpr int Base::* base_p{ static_cast<int Base::*>(derived_p) };
return (base_p == nullptr);
}
The error message with Clang 8.0.0 is the following:
case.cpp:16:33: error: constexpr variable 'base_p' must be initialized by a constant expression
constexpr int Base::* base_p{ static_cast<int Base::*>(derived_p) };
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I noticed that it compiles fine with Clang in two cases:
- remove the constexpr from the last definition
- replace the line
constexpr int Derived::* derived_p{ data_p };withconstexpr int Derived::* derived_p{ &Derived::bar };.
Should the constexpr expression (the one that makes Clang and ICC fail) compile?