Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 596
Ryan Haining Avatar asked Apr 17 '15 22:04

Ryan Haining


1 Answers

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;

like image 146
Columbo Avatar answered Oct 04 '22 02:10

Columbo