Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Isn't the 'modern' solution to memcpy into a temp and swap the bytes in that? C++ has added/will add std::launder and std::bless to deal with this issue


> Isn't the 'modern' solution to memcpy into a temp and swap the bytes in that?

Or just use the endian.h / sys/endian.h routines, which do the right thing (be32dec / be32enc / whatever). memcpy+swap is fine, and easier to get right than the author's giant expressions, but you might as well use the named routines that do exactly what you want already.


>C++ has added/will add std::launder and std::bless to deal with this issue

You're thinking of std::bit_cast. std::launder solves a different, much more obscure problem: https://miyuki.github.io/2016/10/21/std-launder.html


oops, my mistake


No, it is to read a byte at a time and turn it into the semantic value for the data structure you are filling in. Like read 128 and then 1 and set the variable to 32769. If u are the author of protobufs then you may run profiling and write the best assembly etc but otherwise no, don’t do it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: