-2

I try to do a router : router.get("/mypath", callback)

But I have some issue with std::function<void(http::request_parser<http::empty_body>)> is from boost beast

struct Routes {
  typedef  std::function<void(http::request_parser<http::empty_body>)> handler;
};

void get(const std::string& ressource, Routes::handler handler);

router.get("/status", &api::test);

void api::test(http::request_parser<http::empty_body>)
{
    std::cout<<"hello"<<std::endl;
}

But I can not call void function, I don't know why

t.niese
  • 39,256
  • 9
  • 74
  • 101
Vana
  • 753
  • 3
  • 11
  • 20
  • use `boost::bind` https://cs.brown.edu/~jwicks/boost/libs/bind/bind.html#with_function_objects – cprogrammer Apr 03 '19 at 08:45
  • 1
    And is `api::test` a static function of the `api` class? Or free function in a method? What's the error message? – Matthieu Brucher Apr 03 '19 at 08:51
  • Have a look at https://stackoverflow.com/questions/2402579/function-pointer-to-member-function – Matthieu Brucher Apr 03 '19 at 08:51
  • Test is from api class. The error message is `no viable conversion from 'void (api::*)()' to 'Routes::handler' (aka 'function >)>') router.get("/status", &api::test);` – Vana Apr 04 '19 at 04:49

1 Answers1

-1

api::test has 2 params, handler function only one.

You need to "adapt" ypur function by binding a param

cprogrammer
  • 5,503
  • 3
  • 36
  • 56
  • OP can probably use std::bind here, or even better, lambdas. It's also not the problem at all, the callback is `api::test`, not `get`. – Matthieu Brucher Apr 03 '19 at 08:49
  • I prefer use lambda – Vana Apr 03 '19 at 11:18
  • But I don't understand why I must use bind or lambda. `get` take 2 parameters, but the handler 1, the callback take 1, and I need only 1 into callback – Vana Apr 03 '19 at 11:32