mercredi 21 juillet 2021

c++ function polymorphism/template

My goal is to provide three ways calling the function listen(). Is there a better way to do this? (Like using template or default value parameters so I can implement only one listen() function)
My current approach

void listen(const int &port,
                 const std::function<void(std::string err)> &f) {
    http_server_.listen(port, [f](std::string err) { f(err); });
}
void listen(const int &port, const std::function<void()> &f) {
    http_server_.listen(port, [f](std::string err) { f(); });
}
void listen(const int &port) {
    http_server_.listen(port, [](std::string err) {});
}

I have tried the template approach which looks like

template <typename Func> void listen(const int &port, Func f = {}) {
    // do some if-else here.
}

but it doesn't work.

Aucun commentaire:

Enregistrer un commentaire