Does void (*) in C ++ have anything?

I am trying to understand the C ++ function pointer syntax. In Eclipse on Linux, when you type:

void(*); 

He flashed the expression with a syntax error message, but he allowed me to compile it and run the program. Then in Visual Studio I tried it and it won’t compile saying "Expected Expression". However, what is strange when I do this:

 std::vector<void(*)> myVector; 

It compiles fine in Visual Studio. Also a couple of online compilers void (*); excellent on his own work. I know that:

 void (*)(); 

... is a function pointer and ...

 void(); 

... is the signature of the function, so you can:

 std::function<void()> func; 

I am having trouble understanding the syntax of a function pointer.

Thanks.

+5
source share
1 answer

Remember that parentheses can be used to change the priority of certain things. This is why you have brackets around an asterisk in void (*)() , because it is very different from void *() .

In the case of void(*) parentheses are such moving parentheses. But they are not needed. Type void(*) void* , simple and simple.

The context in which you use it is important.

+12
source

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


All Articles