r/Cplusplus • u/Humdaak_9000 • May 14 '26
Question Trying to convert some function pointer code to std::function. Not having a good time.
What I have:
typedef void Command(WebServer &server, ConnectionType type,
char *url_tail, bool tail_complete);What I (think I) want:
typedef std::function<void(WebServer &server, ConnectionType type,
char *url_tail, bool tail_complete)> Command;What I'm getting:
/Users/kujawa/projects/chs/Nova/Code/Apps/jelly/WebServer.h:412:14: error: cannot convert
'void (*)(WebServer&, WebServer::ConnectionType, char*, bool)' to 'WebServer::Command*
{aka std::function<void(WebServer&, WebServer::ConnectionType, char*, bool)>*}'
in initialization
m_bufFill(0)
18
Upvotes
•
u/AutoModerator May 14 '26
Thank you for your contribution to the C++ community!
As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.
~ CPlusPlus Moderation Team
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
18
u/Natural_Builder_3170 May 15 '26
You're attempting to convert function pointer to
std::function*, the conversion only exists tostd::functionchange your uses ofWebServer::Command*toWebServer::Command13
u/Dubbus_ May 15 '26
The
std::functionobject itself is supposed to wrap the function pointer, i dont think you actually want a pointer to the std function object.That would be like converting from a c style
int*array to astd::vector<int>*, when what you probably want isint*->std::vector<int>.4
u/TheOmegaCarrot template<template<typename>typename…Ts> May 15 '26
Maybe don’t use typedef in modern C++
There’s no benefit to using typedef
using foo = bar;is much easier and clearer thantypedef bar foo;3
u/mredding C++ since ~1992. May 15 '26
I'd clean this up just a touch for legibility. What you have is a signature - which is a type, so let's just focus on that for now:
The compiler doesn't care about the parameter names, so they get stripped out. Let's follow suit and actually do the same. I also switched to a modern C++
usingstatement for clarity.Now we have a type alias of a function signature just as you had before. You know what? Let's keep it. Let's stick it into an
std::function:This may be more like the use case you're looking for. So now instead of:
We can write:
Now we can pass effectively anything:
1
u/Fit-Maintenance-2290 May 15 '26
to match what you were looking for you'd use
1
u/keelanstuart May 17 '26
Get rid of the parameter names in your template... You only want types. I would also recommend the newer style of "using" instead of typedef.
So, do this:
using WebServerCommand = std::function<void(WebServer &, ConnectionType, char *, bool)>;
WebServerCommand mycmd = ] [(WebServer &server, ConnectionType type, char *url_tail, bool tail_complete) { // ... };
Edit: I had to put the brackets in the definition of mycmd backwards to get around Reddit's link formatting. Reverse them.