1

Sorry for possible duplicates. Because I couldn't find the answer

So my question is. I am trying to get an address to the function

void func() {}

void* funcPtr = func;

I am using C++ 17 at Visual Studio and clang giving me that warning

implicit conversion between pointer-to-function and pointer-to-object is a microsoft extension[clang-diagnostic-microsoft-cast]

I want to use it, to check if two pointer to a function are actually pointing to the same function. For example:

const std::function<void()>& stdFunc1 = func;
const std::function<void()>& stdFunc2 = func;

void* funcAdr1 = *stdFunc1.target<void(*)()>();
void* funcAdr2 = *stdFunc2.target<void(*)()>();

bool test = funcAdr1 == funcAdr2;

What clang trying me to tell. And what possible workaround can I do? For my project it is not really necessary. But I want to know it for future.

  • You're staring at the answer yourself. The answer to your question lies somewhere in the "`target`" part. That's what the compiler is barking about. – Sam Varshavchik Dec 28 '22 at 02:13
  • 1
    See [here](https://en.cppreference.com/w/cpp/language/pointer#Pointers_to_void). According to the standard, only pointers to objects are guaranteed to be convertible to and from `void*`. – Nathan Pierson Dec 28 '22 at 02:14
  • Data pointer ≠ function pointer. (Also, ≠ member function pointer.) – Eljay Dec 28 '22 at 04:49

0 Answers0