如何在模板化类中为模板化函数创建函数回调。以下是我到目前为止的情况:
template <typename SocketType>
class NettyPBSslSerializer {
...
template <typename Handler>
void async_read(google::protobuf::Message *pbmess,Handler handler ) {
void (NettyPBSslSerializer<SocketType>::*f1)(
const boost::system::error_code&,std::size_t offset,google::protobuf::Message *pbmess,boost::tuple<Handler>)
=&NettyPBSslSerializer<SocketType>::async_read_varint <Handler> ;
boost::asio::async_read (socket_,
boost::asio::buffer(&read_buffer_.at(0), 9),
boost::asio::transfer_at_least(1),
boost::bind(f1,this,boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, pbmess,boost::make_tuple(handler) )
);
}
template <typename Handler>
void async_read_varint(const boost::system::error_code& e, std::size_t offset,google::protobuf::Message *pbmessp, boost::tuple<Handler> handler) {
}
....
SocketType socket_;
}
使用g ++
我收到以下错误 nettypbserializer.hpp|186| error: expected primary-expression before '>' token
nettypbserializer.hpp|186| error: expected primary-expression before ';' token
第186行
=&NettyPBSslSerializer<SocketType>::async_read_varint <Handler> ;
答案 0 :(得分:1)
您不能指向(成员)函数模板,因为它尚不存在。一种解决方案是将Handler
类型参数移至NettyPBSslSerializer
(以及NettyPBSslSerializer<SocketType, HandlerType>
),然后绑定&NettyPBSslSerializer::async_read_varint
...
编辑:
实际上很容易。您不能将指针指向成员函数模板,但您可以构建一个闭包,此函数模板将使用正确的类型进行实例化,并将结果传递给boost::async
。这是一个conceptual implementation:
#include <iostream>
namespace boost {
template <typename Stream, typename Handler>
void async_read(Stream p_Stream, Handler p_Handler)
{
p_Handler();
}
}
template <typename SocketType>
class NettyPBSslSerializer {
SocketType m_Socket;
template <typename Handler>
void async_read_varint(Handler handler) {
handler();
}
public:
template <typename Handler>
void async_read(Handler handler) {
boost::async_read(m_Socket, [&]() -> void {
this->async_read_varint(handler);
});
}
NettyPBSslSerializer(SocketType const & p_Socket) : m_Socket(p_Socket) {}
};
void a_handler()
{
std::cout << "a_handler called" << std::endl;
}
int main()
{
NettyPBSslSerializer<int> tTmp(42);
tTmp.async_read(a_handler);
}