Reputation: 14434
I know in many cases it's best not to ask why, but this function signature makes no sense to me. I know we're supposed to use typedefs, but I wanted to see if I could understand how to do it without:
bool(*fn)(std::string&); // This is function pointer
void funcTakingFunc1(bool(*)(std::string&)) { } // This works for function arguments
bool(*)(std::string&) funcReturningFunc() {} // Trying the same as return type, doesn't work
bool(*funcReturningFunc2(std::string&))() {} // This is what I found on another SO question
The last one I think is right, it makes no sense to me and the function name and the arguments for the 'return function' are switched around left to right. Is there any explanation for this?
Specifically I'm trying to make a function that takes an std::string&
, and returns a function pointer to a bool (*)(std::string&)
, and this doesn't work:
bool (*getConsoleCmdByName)(std::string&)(std::string& cmd);
Edit: It turns out this is correct, I think:
bool (*getConsoleCmdByName(std::string&))(std::string& cmd);
Jarod suggested:
auto getConsoleCmdByName(std::string&) -> bool (*)(std::string&)
As a possibility, that seems clear enough for me.
Upvotes: 2
Views: 2584
Reputation: 15524
Old school pointer-to-function declarations are notoriously difficult to read.
C++11 Provided the standard library with some type-support tools which can be used to simplify complex declarations, e.g.:
using ret_t = std::add_pointer_t<bool(std::string&)>;
using func_ptr_t = std::add_pointer_t<ret_t(std::string&)>;
Upvotes: 0
Reputation: 6465
For this purposes and easier understanding complicated expressions exists Clockwise/spiral rule
+---------------------------------+
| |
| +----------+ |
| ^ | |
bool(*funcReturningFunc2(std::string&))() {}
^ ^ | |
| +------------------+ |
| |
+--------------------------------------+
You may ask, what is funcReturningFunc2?
bool
Upvotes: 5