简体   繁体   中英

How can I see if a function pointer passed to a function returns void?

I have a function taking a function pointer, and I want to store the return value if the function pointed to by the pointer returns, and just run the function otherwise. Something like this:

static void threadStart(void (*function)(void*), void* arg) {                            
      if(function does not return void)  (void*) store = function(arg);  
       else(function())
};

How can I do this? I tried just doing:

static void threadStart(void (*function)(void*), void* arg) {                            
   (void*) store = function(arg);  
};

But this produces a compilation error.

You can't.

Because function is declared as void (*function)(void*) it always returns void, so you don't need to.

As other answers have said, it's not possible.

Looking at what's being attempted, you should alter function 's signature so it returns a datatype with a sentinel value. For example, function could return a void* and you could check that it is not NULL .

So the parameters to threadStart would become (void* (*function)(void*), void *arg) . This matches a common design pattern used in libraries like pthread_create , which in its case has parameters (void *(*start_routine) (void *), void *arg) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM