1

I want to cast a template function's address to void*:

template<typename T>
int prt(T a)
{
    return ++a;
}

void* ptr = static_cast<void*>(&prt<int>);

In MSVC compiler, it works, but in clang it causes a compilation error:

address of overloaded function 'prt' cannot be static_cast to type 'PVOID' (aka 'void *')

Why doesn't clang allow this?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Barracudach
  • 121
  • 3
  • 1
    Does this answer your question? [Why can't I cast a function pointer to (void \*)?](https://stackoverflow.com/questions/36645660/why-cant-i-cast-a-function-pointer-to-void). On some architectures function pointers can be different from regular pointers. – dewaffled Sep 29 '22 at 23:51
  • In many cases you can still do this, but it requires a `reinterpret_cast`. You should also combine this with a static_assert that ensures this fits in a pointer type. – paddy Sep 29 '22 at 23:56
  • Maybe better (c++) dup - https://stackoverflow.com/q/1096341/4074081 – dewaffled Sep 29 '22 at 23:57
  • If you disable language extensions `/Za` the Microsoft compiler fails as well. https://godbolt.org/z/7dP9cYzKT – Retired Ninja Sep 30 '22 at 00:00
  • 1
    @dewaffled that question is the opposite of this one. That one asks about casting a `void*` to a function pointer. This question asks about casting a function pointer to a `void*`. – Remy Lebeau Sep 30 '22 at 00:14

0 Answers0