[Boost-users] boost::endian::native_to_big_inplace fails with char

7 views
Skip to first unread message

Martijn Otto via Boost-users

unread,
Mar 18, 2019, 12:25:24 PM3/18/19
to boost...@lists.boost.org, Martijn Otto
I am having issues with boost::endian:native_to_big_inplace when used
with a char. Whatever you try to 'reverse' comes out as 0.

Looking through the code, on little endian machines it calls
endian_reverse (as expected). This function has no overload for a char
and it then gets promoted to an int.

Since the int _is_ actually larger than 1 byte, something gets
reversed. The native_to_big_inplace then returns it as a char,
narrowing the int and throwing away all the relevant bits, resulting in
a 0.

I found an issue on github about this: https://github.com/boostorg/endi
an/issues/10 about exactly this issue. The bug is easily shown if you
try to compile

static_assert(sizeof(boost::endian::endian_reverse('y')) ==
sizeof('y'))

signature.asc
Reply all
Reply to author
Forward
0 new messages