1

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.

1 Answer 1

2

You cannot perform arithmetic on just any pointer in constexpr.
You can perform arithmetic on pointers to arrays (or objects as one sized arrays) as long as you don’t end up outside the array.

Sign up to request clarification or add additional context in comments.

2 Comments

...or further than one element beyond the end of the array.
Do the following two statements lead to your answer? If this is the case could you add them to your answer? (Statement 1) Paragraph 4 in section 8.5.6 [expr.add] of n4713 says that static_cast<void*>(static_cast<char*>(nullptr)+1) gives undefined behaviour. (Statement 2) Paragraph section 8.6 [expr.const] item (2.6) of paragraph 2 states that if pointer arithmetics leads to undefined behavior this is not a core constant expression. So constexpr is not applicable to this expression and leads to an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.