Error in returning function pointer in C ++ without typedef

I am trying to return a pointer to a function without using typedef, but the compiler (gcc) throws a strange error, as if I could not make such a setting.

Notes: using typedef code works.

the code:

void catch_and_return(void (*pf)(char*, char*, int&), char *name_one, char* name_two, int& number)(char*, char *, int&) { pf(name_one, name_two, number); return pf; } 

Error:

'catch_and_return' is declared as a function returning a function

Can you explain to me why the compiler does not allow me to do this? Thanks!

+4
source share
1 answer

Declare your function as follows:

 void (*catch_and_return(void (*pf)(char*, char*, int&), char *name_one, char* name_two, int& number))(char*, char *, int&) { pf(name_one, name_two, number); return pf; } 

The syntax for functions returning functions is:

return-function-return-type (* function-name (parameter list)) (function-to-return-parameter-list)

Note: These declarations can be cumbersome to understand at a glance, use typedef when possible.

+7
source

Source: https://habr.com/ru/post/1493967/


All Articles