繁体   English   中英

boost asio async_read文档

[英]boost asio async_read documentation

示例与async_read 手册之间存在不一致之处。 根据手册, async_read 处理程序需要2个参数:错误代码和传输的字节数,但是在该示例中,处理程序仅接受错误代码。 这里发生了什么?

boost::bind生成的类型仍然满足ReadHandler的要求。 Boost.Bind的文档指出,多余的参数会被忽略。

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

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

有关bind的更好说明和解释,请考虑阅读博客条目。 其插图之一特别显示了多余论证的情况。

既然可以,请使用超过N个参数调用绑定对象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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM