Is the following program formally correct C++?
#include <iostream>
#define CASE 1
#if CASE==1
constexpr void* crash = static_cast<void*>(static_cast<char*>(nullptr)+1);
#elif CASE==2
constexpr void* crash = nullptr;
#elif CASE==3
const void* crash = static_cast<void*>(static_cast<char*>(nullptr)+1);
#endif
int main() {
std::cout << "Crash: " << crash << "\n";
}
G++ 8.4.0 reports
error: reinterpret_cast from integer to pointer
constexpr void* crash = static_cast<void*>(static_cast<char*>(nullptr)+1);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Visual Studio 19 cl
reports:
null.cc(3): error C2131: expression did not evaluate to a constant
null.cc(3): note: failure was caused by unevaluable pointer value
How are those error messages to be interpreted?
If I change the value of CASE
to 2 or 3 the compilation succeeds with both compilers and a run of the compiled program gives the expected results.