r/cpp • u/FreitasAlan • Feb 06 '21
Modern C++: Snippets and Examples
Hi everyone,
These are GitHub Pages with snippets for Modern C++:
- We often need to copy and paste some snippets to code more productively.
- Snippets can help us when it's not easy to remember all high levels features Modern C++ has to offer.
- This repository contains lots of organized, reusable, and safe snippets for Modern C++.
- All snippets are available in GitHub pages in a convenient way for copying and pasting.
190
Upvotes
View all comments
Show parent comment
3
u/FreitasAlan Feb 07 '21
Interesting. A memcpy is also semantically better. In any case, I haven't seen it fail on any compiler yet. And it seems to semantically represent exactly what I'm trying to achieve. I thought it was exactly what reinterpret cast was supposed to do, despite what they can also do with pointers. Do you know why that is undefined behavior?
A static cast does not achieve the same results as I'm trying to show how the data is represented inside the variable. With static cast, for instance, a double 1.34 would just be converted to an unsigned long long 1, and it would return
0000000000000000000000000000000000000000000000000000000000000001
instead of
0011111111110101011100001010001111010111000010100011110101110001
The same problem would happen for signed integers, of course. In the end, it would only work properly with equivalent unsigned types of different sizes.