std :: bind在boost :: asio :: ip :: tcp :: resolver上失败

时间:2014-07-11 05:47:33

标签: c++ boost boost-asio

有人可以告诉我发生了什么吗

#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();
}

编译错误

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.

1 个答案:

答案 0 :(得分:0)

最后我可以编译。 试试这个:

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, ...);
...
}