This is more or less a request for clarification on Casting a function pointer to another type with code example
struct my_struct; void my_callback_function(struct my_struct* arg); void do_stuff(void (*cb)(void*)); static void my_callback_helper(void* pv) { my_callback_function(pv); } int main() { do_stuff(&my_callback_helper); }
The response says that the “good” compiler should be able to optimize my_callback_helper(), but I did not find the compiler at https://gcc.godbolt.org , and the helper function is always generated, even if it just jumps tomy_callback_function() (-O3):
my_callback_helper()
my_callback_function()
my_callback_helper: jmp my_callback_function main: subq $8, %rsp movl $my_callback_helper, %edi call do_stuff xorl %eax, %eax addq $8, %rsp ret
So my question is: is there anything in the standard that prevents compilers from excluding the helper?
, . , " ".
my_callback_helper. , , do_stuff. , do_stuff , , /call my_callback_function (my_callback_helper). my_callback_helper, , do_stuff. do_stuff - , . , , do_stuff .
my_callback_helper
do_stuff
my_callback_function