Search code examples
c++clanglanguage-lawyerc++14constexpr

Is the result of static_casting a constexpr void* a constant expression?


clang is rejecting this code which gcc allows:

int main() {
    static constexpr const void *vp = nullptr;
    static constexpr const char *cp = static_cast<const char*>(vp);
}

With the following:

error: constexpr variable 'cp' must be initialized by a constant expression
  static constexpr const char *cp = static_cast<const char*>(vp);

After reading the final listing in N3797 5.9/2 I don't see anything that forbids static_cast's use in a constant expression. Am I looking in the wrong place or misreading something? Or should I open a bug against clang?


Solution

  • Well, the C++14 standard (and your (non-final) draft!) mandates that

    A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions:

    — a conversion from type cv void * to a pointer-to-object type;