1

in the following code i make map,
then add some httprequest class instances to it,
then i call boost asio spawn using member function of httprequest class,
i asked about this before and told it was duplicate ,
so i read the post and made some digging,and found the following:
i have to use boost::bind to convert member function and its arguments to single function object.
and inside boost bind i have to add address of the instance on which the method works,and address of method"do not get if address of method from class definition or from the used instance,
then add the arguments. my map has httprequests represented by shared pointers.
when i write this and compile it gives me lots of errors related to bind.
the problem is stated to be in bind file which represent my code but does not say what is the part having problem.

here is the code:

std::map<std::string, boost::shared_ptr<HTTPRequest>> requests_variables;
std::map<std::string, boost::shared_ptr<HTTPResponse>> tasks;    

for (int hour = 0;hour < 24;hour++)
{
    std::stringstream ss_hour;
    ss_hour << std::setw(2) << std::setfill('0') << hour;
    std::string url_hour{ ss_hour.str() };
    std::stringstream().swap(ss_hour); // swap m with a default constructed stringstream
    URL = URL + url_hour +"h_ticks.bi5";

    std::string request_name = "request_" + std::to_string(hour);
    requests_variables[request_name] =  client.create_request(hour, URL);       
    boost::asio::spawn(client.get_service_reference(), boost::bind(&(*requests_variables[request_name]),&(*requests_variables[request_name]).execute, boost::placeholders::_1, tasks, request_name, requests_variables));

i tried to manipulate it many times to get it compile but in vain.
something is wrong with the bind but i can't get to it.
thanks in advance..

ahmed allam
  • 377
  • 2
  • 15

1 Answers1

0

i do not know why, but this made it work.

HTTPRequest* requst_z = requests_variables[request_name].get();

        boost::asio::spawn(client.get_service_reference(), boost::bind(&HTTPRequest::execute, requst_z, boost::placeholders::_1, tasks, request_name, requests_variables));
ahmed allam
  • 377
  • 2
  • 15
  • Time to read a C++ book, I guess. You should be able to spot the difference between `&(*x_ptr).execute` and `&X::execute`. So then you _do_ know why it makes it work. `&(*x_ptr).execute` tries to find a member (function) on a ... pointer. That doesn't work. `(*x_ptr).execute(arg1, arg2)` would _execute_ that member directly (just like the more usual syntax `x_ptr->execute(arg1, arg2)`) but that's not what you want: You want to be able to execute that function **later** which is why you bind `&X::execute` with some arguments and some placeholders. – sehe Apr 22 '18 at 15:24
  • You can bind any ***callable***. Exactly what is a calleable is described in the documentation for Boost Bind, as well as here: http://en.cppreference.com/w/cpp/concept/Callable – sehe Apr 22 '18 at 15:24
  • 1
    thanks a lot for your explain...the word "directly"made it clear...thanks again – ahmed allam Apr 22 '18 at 15:46