2012-01-21 5 views
1

여기에서 내 문제를 파악하는 것은 매우 일반적인 일이지만, 여기서 내가 잘못하고있는 것을 꽤 볼 수는 없습니다.부스트 바인드/부스트 기능에 문제가있는 튜플을 부스트합니까?

저는 boost :: asio 항목을 수행하고 템플릿 기반 비동기 읽기 함수를 작성하려고합니다.

여기에 함수가 있습니다.

template <typename Handler> 
void AsyncRead(std::vector<boost::uint8_t>& _inMsg, Handler _handler) 
{ 
    #if debug 
     std::cout<< "IConnection::AsyncRead" << std::endl; 
    #endif 
    using namespace protocols; 

    typedef boost::tuple<Handler> tHandler; 
    typedef boost::function< void(const boost::system::error_code &, std::vector<boost::uint8_t> &, tHandler) > HeaderReaderFunc; 

    //void (AConnection::*f)(const boost::system::error_code&, std::vector<boost::uint8_t>&, boost::tuple<Handler>) = &AConnection::HandleReadHeader<Handler>; 


    tHandler t(boost::make_tuple(_handler)); 
    HeaderReaderFunc x(boost::bind(&AConnection::HandleReadHeader<Handler>, this, boost::asio::placeholders::error, boost::ref(_inMsg), t)); 
    inboundHeader.resize(sizeof(SocketIO::MsgData)); 
    boost::asio::async_read(socket, boost::asio::buffer(inboundHeader), x); 

} 

이 기능의 마지막 문에서 문제가 발생하기 시작합니다.

boost::asio::async_read(socket, boost::asio::buffer(inboundHeader), x); 

변수 'x'를 async_read 함수에 매개 변수로 전달하려고 시도합니다.

오류는 길이와 해독 할 수없는 의미에서 전설적입니다. 오류 출력의

그냥 작은 예 : 여기 부스트 기능을 사용하고, 대신 멤버 함수에 대한 참조입니다 주석 라인을 사용하지 않는 경우

boost_1_38_0/boost/asio/detail/bind_handler.hpp: In member function ‘void boost::asio::detail::binder2<Handler, Arg1, Arg2>::operator()() [with Handler = boost::function<void()(const boost::system::error_code&, std::vector<unsigned char, std::allocator<unsigned char> >&, boost::tuples::tuple<boost::function<void()(const boost::system::error_code&)>, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>)>, Arg1 = boost::system::error_code, Arg2 = unsigned int]’: 

는 상황이 제대로 작동하지만, 왜 그런지 내 손가락을 칠 수는 없다.

AConnection :: HandleReadHeader 기능 템플릿 함수 서명은 다음과 같습니다

template <typename Handler> 
void HandleReadHeader(const boost::system::error_code& _e, 
        std::vector<boost::uint8_t> & _inMsg, 
        boost::tuple<Handler> _handler) 

핸들러 유형이다 : AsyncRead 및 HandleReadHeader 내 자신의 클래스 AConnection의 구성원

boost::function<void (const boost::system::error_code &) > 

기능 (아마 중요하지 않음).

어느 내가 부스트 : : 함수가 서명의 개체가 부스트 :: ASIO :: async_read 함수의 세 번째 매개 변수 형식이되지 않는 부스트 :: 튜플, 또는 을 포함을 만드는 점에서 구문에 대한 뭔가를 누락 내 변수 'x'와 (와) 일치합니다.

도움을 주시면 감사하겠습니다. 감사합니다.

편집 : 작업을 수행하지만, 대신 부스트 :: functon의 멤버 함수 참조를 사용

여기

입니다 코드입니다.

boost::function< void(const boost::system::error_code &, 
    std::vector<boost::uint8_t> &, boost::tuple<Handler>) > 

이 서명과 유사한 :

void handler(const boost::system::error_code &, 
    std::vector<boost::uint8_t> &, boost::tuple<Handler>) 

아직, 부스트는 ASIO 문서 핸들러의 서명이 있어야한다 말 : 당신의 변수 x

template <typename Handler> 
void AsyncRead(std::vector<boost::uint8_t>& _inMsg, Handler _handler) 
{ 
#if debug 
    std::cout<< "Connection::AsyncRead" << std::endl; 
#endif 
using namespace protocols; 
// Issue a read operation to read exactly the number of bytes in a header. 
void (Connection::*f)(
    const boost::system::error_code&, 
    std::vector<boost::uint8_t>&, boost::tuple<Handler>) 
    = &Connection::HandleReadHeader<Handler>; 

inboundHeader.resize(sizeof(simple::message_header)); 
boost::asio::async_read(socket, boost::asio::buffer(inboundHeader), 
    boost::bind(f, 
    this, boost::asio::placeholders::error, boost::ref(_inMsg), 
    boost::make_tuple(_handler))); 
} 

답변

0

이 유형이

void handler(const boost::system::error_code&, 
    std::size_t bytes_transferred) 

그래서 서명이 일치하지 않습니다. Boost 문서에서 찾을 수없는 과부하를 사용하고 있거나, async_read가 기대하는 것과 일치하도록 HeaderReaderFunc 유형을 변경해야합니다.

+0

"void (AConnection :: * f) (const boost :: system :: error_code & std :: vector & boost :: tuple )를 사용할 때 왜 작동하는지 궁금합니다. & AConnection :: HandleReadHeader ; "참조? boost :: function을 사용하는 것에 비해 특별한 것에 대해 더 자세히 살펴볼 것입니다. – sbrett

+0

잘 모르겠습니다 ... async_read 호출로 멤버 함수에 대한 포인터를 실제로 사용한 코드를 보지 못했습니다. –

+0

문제 코드를 업데이트하여 사용 사례를 보여주었습니다. 두 코드 간 차이점을 파악하는 데 도움이 될까요? – sbrett

관련 문제