Function pointers casting in C++

前端 未结 8 1402
星月不相逢
星月不相逢 2020-11-27 15:45

I have a void pointer returned by dlsym(), I want to call the function pointed by the void pointer. So I do a type conversion by casting:

void *gptr = dlsym(         


        
8条回答
  •  死守一世寂寞
    2020-11-27 16:22

    well, if you know what the argument list is, it's extremely simple to just c-cast it. As stated above, it has undefined behavior, but I've been using this in my own events handler for a pet project and it seems to work just fine on msvc.
    I can cast the same void* to _beginthread_proc_type to start a thread with _beginthread, and that also doesn't seem to cause any issues (though I don't really know what the consequences of sending arguments to a function that doesn't require any, or not sending arguments to functions that do require arguments will do, that does seem to at least call the function/start the thread in my limited testing)

    void somefunction(){
        std::cout <<"hi"<

    I know the community has developed a hatred for macros, but I use a macro for that function call in my events handler.

    #define call_voidstar_function(fc)     ((void(__cdecl*)(void))(fc)) ()
    

提交回复
热议问题