https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118173

            Bug ID: 118173
           Summary: reinterpret_cast<void(*)()> from const void* casts
                    away constness, but is accepted
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: janschultke at googlemail dot com
  Target Milestone: ---

> const void* null = nullptr;
> auto n = reinterpret_cast<void(*)(void)>(null);

https://godbolt.org/z/Mbajjfn6n

GCC accepts this invalid code (as does MSVC); Clang rejects.

By definition in https://eel.is/c++draft/expr.const.cast#6, this code is
ill-formed because the conversion from 'const void*' to 'void(*)()' casts away
constness. https://eel.is/c++draft/expr.reinterpret.cast#2 disallows
reinterpret_cast from doing so.

While function types cannot be const-qualified anyway, there is no special rule
in the standard for them here, and this is dropping const as much as 'const
void*' -> 'void*'.

See standardese explanation here:
https://github.com/cplusplus/CWG/issues/657#issuecomment-2558598637

Reply via email to