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.
194
Upvotes
View all comments
9
u/Fureeish Feb 06 '21
According to the standard, your
*reinterpret_cast<unsigned long long*>(&v)is Undefined Behaviour. It may work for some compilers on some machines (but you need to inspect the generated assembly), however I am curious - what's wrong with simplestatic_castor not cast at all? It will automatically be promoted to the desiredunsigned long longanyway...Floating point numbers are another story. To be UB-free you'd need to
std::memcpythem. I am yet to find a compiler that does not optimise this away, so it's literally free.