-1

Can somebody enlighten me what's going on

#include <functional>
#include <boost/asio.hpp>

int main() {
    typedef boost::asio::ip::tcp::resolver resolver;
    boost::asio::io_service io;
    resolver res(io);

    // typical way to call the resolver is (this works)
    // res.async_resolve(resolver::query("google.com", ""), [](const boost::system::error_code &, resolver::iterator){});

    // this line fails with 'no matching function call to bind'
    auto bound_resolver = std::bind(&resolver::async_resolve, res, resolver::query("google.com"), std::placeholders::_1);
    bound_resolver([](const boost::system::error_code &, resolver::iterator){});

    io.run();
}

The compile error

test.cpp:13:27: error: no matching function for call to 'bind'
    auto bound_resolver = std::bind(&resolver::async_resolve, res, resolver::query("google.com"), std::placeholders::_1);
                          ^~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/functional:1623:5: note: candidate template ignored: couldn't infer template argument '_Func'
    bind(_Func&& __f, _BoundArgs&&... __args)
    ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/functional:1650:5: note: candidate template ignored: couldn't infer template argument '_Result'
    bind(_Func&& __f, _BoundArgs&&... __args)
    ^
1 error generated.
dpington
  • 1,844
  • 3
  • 17
  • 29
  • You'll need to disambiguate the function as `async_resolve` is overloaded. The signature for `async_resolve` is pretty hardcore, are there no examples of this in the documentation? – user657267 Jul 11 '14 at 07:26
  • 3
    There are 2 issues in your code: 1) `std::bind` copies its arguments, but `resolver` is not copyable, so either pass a pointer to it or wrap with `ref(res)`; 2) `async_resolve` is an overloaded function template, you have to instantiate/type-cast it explicitly. – Igor R. Jul 11 '14 at 07:42

1 Answers1

0

Finally I could compile. Try this:

typedef boost::asio::ip::tcp::resolver resolver;

class AsyncResolveFunction {
  public:
    template <typename ResolveHandler>
    void operator()(resolver *res, const resolver::query &q, ResolveHandler handler) {
        res->async_resolve(q, handler);
    }
};

int main() {
...
    auto bound_resolver = std::bind(AsyncResolveFunction(), &res, ...);
...
}
ALittleDiff
  • 1,191
  • 1
  • 7
  • 24
  • I've tried that already. Same compile error, note that according to the spec both `res` and `&res` are acceptable. http://stackoverflow.com/a/15264126/516037 – dpington Jul 11 '14 at 05:57
  • I've also tried that before and it also does not compile. – dpington Jul 11 '14 at 06:10
  • @HerpDerpington: The reason why you couldn't compile is that `async_resolve` function is a template function. I found a workaround and updated it. Can you try this version? – ALittleDiff Jul 11 '14 at 07:23