Aperson123
Aperson123

Reputation: 129

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.

Upvotes: 0

Views: 92

Answers (2)

Winfield Chen
Winfield Chen

Reputation: 111

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).

Upvotes: 1

You can't.

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

Upvotes: 4

Related Questions