提升asio async_read文档

时间:2013-01-10 11:57:08

标签: c++ boost-asio

this示例与async_read manual之间存在不一致。根据手册async_read handler需要2个参数:错误代码和传输的字节数,但在示例中,处理程序只接受错误代码。这是怎么回事?

2 个答案:

答案 0 :(得分:2)

boost::bind生成的结果类型仍符合ReadHandler的要求。 Boost.Bind的文档声明会忽略额外的参数。

例如,在Boost.Asio的内部,ReadHandler调用链可能类似于:

handler( error, bytes_transferred ) 
`-- binder::operator()( error, bytes_transferred )
    `-- bound_function( error )

为了更好地说明和解释bind,请考虑阅读this博客条目。其中一个例子专门展示了剩余论证的情况。

答案 1 :(得分:1)

因为没关系,用超过N个args调用bind对象operator()。

#include <iostream>
#include <boost/bind.hpp>

template<typename T>
void call(const T& f)
{
   f(1, 2, 3, 4);
}

void f(int i) { std::cout << i << std::endl; }

int main()
{
   call(boost::bind(&f, 1));
}

http://liveworkspace.org/code/1MrPTQ $ 2

template<class R, class F, class L> class bind_t
{
public:

    typedef bind_t this_type;

    bind_t(F f, L const & l): f_(f), l_(l) {}

#define BOOST_BIND_RETURN return
#include <boost/bind/bind_template.hpp>
#undef BOOST_BIND_RETURN

};

result_type operator()()
{
    list0 a;
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

result_type operator()() const
{
    list0 a;
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

template<class A1> result_type operator()(A1 & a1)
{
    list1<A1 &> a(a1);
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

template<class A1> result_type operator()(A1 & a1) const
{
    list1<A1 &> a(a1);
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

template<class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9> result_type operator()(A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5, A6 const & a6, A7 const & a7, A8 const & a8, A9 const & a9)
{
    list9<A1 const &, A2 const &, A3 const &, A4 const &, A5 const &, A6 const &, A7 const &, A8 const &, A9 const &> a(a1, a2, a3, a4, a5, a6, a7, a8, a9);
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

template<class A1, class A2, class A3, class A4, class A5, class A6, class A7, class A8, class A9> result_type operator()(A1 const & a1, A2 const & a2, A3 const & a3, A4 const & a4, A5 const & a5, A6 const & a6, A7 const & a7, A8 const & a8, A9 const & a9) const
{
    list9<A1 const &, A2 const &, A3 const &, A4 const &, A5 const &, A6 const &, A7 const &, A8 const &, A9 const &> a(a1, a2, a3, a4, a5, a6, a7, a8, a9);
    BOOST_BIND_RETURN l_(type<result_type>(), f_, a, 0);
}

template<class R, class F, class A1>
    _bi::bind_t<R, F, typename _bi::list_av_1<A1>::type>
    BOOST_BIND(F f, A1 a1)
{
    typedef typename _bi::list_av_1<A1>::type list_type;
    return _bi::bind_t<R, F, list_type> (f, list_type(a1));
}

例如boost :: bi :: list1

的实现operator()
template<class R, class F, class A> R operator()(type<R>, F & f, A & a, long)
{
    return unwrapper<F>::unwrap(f, 0)(a[base_type::a1_]);
}

使用一个参数调用f,无论实际捐赠的元素数量是多少。