
struct Packet { internet::IpHeader ipHeader; internet::UdpHeader udpHeader; UserMessage userMessage; }; // Packet
I do see one problem with this - you have defined your struct with a specified endianness. However, if you wanted to send it to a different destination which requires a different endianness, you would have to duplicate your data structure. The more I think about it, the more it's clear to me that this represents an example of the wrong separation of concerns (no flame intended - simply a software engineering term): In the typed approach, the endianness becomes the property of the message. However, I think endianness should be the property of the destination, no?
char buf[BIG_ENOUGH]; <snip>
Terry, I like your example as it does indeed seem to make simple things simple. But other than my comment above, could you consider the following (simple) scenario: - read a matrix from disk and for the sake of argument, let's say it's in a big-endian format - read a number representing the number of its dimensions - based on the number of elements, swap each element to the machine's representation Please note that this isn't based on any existing code I have and I just typed it in, so I expect there are syntax errors :) void * data = ...; read(fh, data, size); MatrixHeader * mh = static_cast<MatrixHeader*>(data); std::size_t nDims = swap<big_to_machine>(mh.nDims); std::size_t nElem = std::accumulate( make_iterator<big_to_machine>(mh.dims), make_iterator<big_to_machine>(mh.dims + nDims), std::size_t(1), std::multiply<std::size_t>()); //this is the part I am most interested in how you'd solve using the typed //approach. Ignore the potential alignment issue. double * dataBeg = static_cast<unsigned char*>(data) + sizeof(mh), dataEnd = dataBeg + nElem; swap_in_place<big_to_machine>(dataBeg, dataEnd); //process dataBeg ... I can't help but think that the typed approach will have to be O(N) in both little and big-endian scenarios. Tom