[endian] Alternative implementation of endian_flip

I wrote the attached code years ago. When I noticed the proposed Boost Endian library, I figured now was as good a time as any to share it. My code provides an alternative solution to Beman's boost::integer::endian_flip function that may or may not be a useful improvement. My implementation exposes a single function template, boost::endian::swap, that accepts any type, but only swaps the endianness of 16-bit, 32-bit, and 64-bit types. Features: 1. Slightly more compact. 2. Accepts any type, not just u?int(16|32|64)_t. + This might be useful for unions/structs, e.g., MSVC's LARGE_INTEGER, and when writing function templates. - Decreases the chance that the compiler can identify improper use (e.g., "swap(my_64_bit_object);"). 3. Uses bit shifting/masking instead of a temporary variable. ~ I'm not sure if this is any better or worse from the compiler's perspective. - My 64-bit implementation may not work on older compilers. -Mike

I wrote the attached code years ago. When I noticed the proposed Boost Endian library, I figured now was as good a time as any to share it. My code provides an alternative solution to Beman's boost::integer::endian_flip function that may or may not be a useful improvement. My implementation exposes a single function template, boost::endian::swap, that accepts any type, but only swaps the endianness of 16-bit, 32-bit, and 64-bit types.
Hi Mike, Not to discourage you but I tried something similar a few months ago - I argued that the proposed endian library should be built on top of simpler swap() and swap_in_place() primitives. I also put together a quick implementation of the proposed interfaces (which, had a few issues) but in the end it seemed that most people here simply wanted to use endian types. I am sure you can find the posts pretty easily, they were from last July-ish. Good luck, Tom
participants (2)
-
Mike Patterson
-
Tomas Puverle