I'm trying understand C++ function pointer syntax. In Eclipse on Linux when I typed:
void(*);
It highlighted the statement with a message saying syntax error, but it let me compile it and the program ran. Then on Visual Studio I tried it and it won't compile, saying "Expected an expression". However what's strange is that when I do:
std::vector<void(*)> myVector;
It compiles fine on Visual Studio. Also on a couple of online compilers void(*); on its own works fine. I know that:
void (*)();
... is a function pointer and..
void();
... is a function signature, which is why you can do:
std::function<void()> func;
I'm having a lot of trouble understanding function pointer syntax.
Thanks.
Remember that parentheses can be used to change the precedence of certain things. That's why you have the parentheses around the asterisk in
void (*)()
because it's very different fromvoid *()
.In the case of
void(*)
the parentheses are such precedence-changing parentheses. But they are not needed. The typevoid(*)
isvoid*
, plain and simple.The context where you use it is important though.