Boost logo

Boost :

Subject: [boost] hton/ntoh template functions
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-09-25 15:03:53


currently most of the applications that must take care of endianess use
ntohl/htonl ntohs/htons functions.
When the size of the integer change we need to change between the -s and -l
functions (or maybe remove the call for chars).

I'm wondering if two template functions would improve the maintenability of
the code.

template <typename T>
T host_to_network(T v);
template <typename T>
T network_to_host(T v);

template <>
long host_to_network<long>(long v) {return htonl(v);}

template <>
long network_to_host<long>(long v) {return ntohl(v);}

template <>
short host_to_network<long>(short v) {return htons(v);}

template <>
short network_to_host<long>(short v) {return ntohs(v);}

we will need to add specialization for all the other integer types, and we
can also include char and unsigned char

template <>
char host_to_network<char>(char v) {return v;}

template <>
char network_to_host<char>(char v) {return v;}

Do you think that these template functions should be included in boost?

Best regards,


Boost list run by bdawes at, gregod at, cpdaniel at, john at