Why Are Callbacks of the HttpRequestHandler Classes Declared as Rvalue References?

chiefsucker • 2 months ago

Basically what the title says, I wonder what the reason is behind the callbacks being declared as rvalue references?

antao2002 • 2 months ago

When a user has to capture the callback for an asynchronous interface, it can be 'moved' (not copied). for example:

auto callbackPtr=std::make_shared<.....>(std::move(callback));  
dbclient<<"select ...."
               >>[callbackPtr](result ...)
                {//create response....
                      *callbackPtr(resp);
                }
                >>[callbackPtr](....)//exception callback, some error occurs.
                {
                 .....
                     *callbackPtr(badresp);
                }
Moving is always cheaper than copying, so I pass the callback as a r-reference.

Log in to comment