Skip to content

reinterpret_cast from const void* to function pointer does not compile #134867

@StilesCrisis

Description

@StilesCrisis

GCC and MSVC both allow a reinterpret_cast to convert from a const void* to void (*)(void*).

Clang refuses because we are "casting away qualifiers" (const), but there is no qualifier to add on the function-pointer side. There is no syntax for assigning values into a function pointer and there is no possible addition of const into the function pointer type which makes the cast allowed.

This can be worked around by using const_cast to strip the const off the void*, or by using a C-style cast, but this seems like it shouldn't be necessary, and no other mainstream compiler requires it.

Example: https://godbolt.org/z/7rxqss7nT

using FuncPtr = void (*)(void*);

extern "C" const void* Lookup(const void* OriginalFunction);

FuncPtr MakeVoidFuncPtr(const void* Func) {
  return reinterpret_cast<FuncPtr>(Lookup(Func));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"diverges-from:gccDoes the clang frontend diverge from gcc on this issuediverges-from:msvcDoes the clang frontend diverge from msvc on this issuequestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions