初始化asio :: ip :: address_v6()的最快方法?

时间:2012-04-19 02:37:45

标签: c++ boost initialization ip-address boost-asio

asio::ip::address_v6为参数提供bytes_type,基本上是网络字节顺序的boost::array

我在void *变量中有一个RAW IPv6地址。将void *转换为asio::ip::address_v6的最快方法是什么?最好使用构造函数。

1 个答案:

答案 0 :(得分:4)

你不能比初始化asio::ip::address_v6::bytes_type更好,实际上可以是std::arrayboost::array

// We need an unsigned char* pointer to the IP address
unsigned char *youraddr = reinterpret_cast<unsigned char*>(your_void_ptr);
asio::ip::address_v6::bytes_type myaddr;
// Copy the address into our array
std::copy(youraddr, youraddr + myaddr.size(), myaddr.data());
// Finally, initialize.
asio::ip::address_v6 ipv6(myaddr);

请注意,如果您能够修改该结构,最好直接存储bytes_type而不是void*