https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99176
Bug ID: 99176
Summary: GCC rejects const_cast of null pointer in constant
expressions
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: richard-gccbugzilla at metafoo dot co.uk
Target Milestone: ---
GCC rejects:
constexpr const int *p = nullptr;
constexpr int *q = const_cast<int*>(p);
saying:
<source>:2:20: error: conversion of 'const int*' null pointer to 'int*' is not
a constant expression
2 | constexpr int *q = const_cast<int*>(p);
| ^~~~~~~~~~~~~~~~~~~
I don't think any such rule exists, and other compilers accept. This only
appears to affect const_casts of null pointers; non-null pointer const casts
seem to work OK. Perhaps GCC thinks that this is a reinterpret_cast / cast from
void* or something like that?
It looks like this regressed between GCC 6 and GCC 7.