C ++, preprocessor operator

Can someone explain what this operator does?

#define CONST_SIG (void (*) () ) 1 
+4
source share
3 answers

This statement defines CONST_SIG as 1 , converted to a pointer to a function that takes no parameters and returns void . This can be useful if you have a pointer to a function, and you might be checking for truth, then CONST_SIG will be true.

You can try the cdecl program, which is available on many linux distributions, to "translate into English" C declarations. Example output in this case:

 cdecl> explain (void (*) () ) cast unknown_name into pointer to function returning void 

Side note: the reason it says "unknown_name" is because our pointer does not have a name. To name it, for example, "p", it will look like this: (void (*p) () ) .

+3
source

You can pass CONST_SIG to a function that expects a function pointer, and treats the value 1 as a special value for that function pointer.

+3
source

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


All Articles