3 ms·
> we already have memcpy, but bit_cast is safer and also can run at compile time. Can someone smarter than I am explain the significance about bit_cast running
by tomcam 9y ago
> we already have memcpy, but bit_cast is safer and also can run at compile time.
Can someone smarter than I am explain the significance about bit_cast running at compile time?
- 0xQSL 9y agoIt can be used with constexpr (http://en.cppreference.com/w/cpp/language/constexpr http://en.cppreference.com/w/cpp/language/constexpr) to precompute values at compile time. Here are some videos on the topic: https://www.youtube.com/watch?v=PJwd4JLYJJY https://www.youtube.com/watch?v=PJwd4JLYJJY https://www.youtube.com/watch?v=rpn_5Mrrxf8 https://www.youtube.com/watch?v=rpn_5Mrrxf8
- Jare 9y agoI suppose to be used in constexpr functions.
- mediocrebs 9y agoSo you can have a constexpr (compile-time) code fragment that, say, casts a float to an unsigned int and extracts the mantissa. Mostly though, this is fairly unimportant and tedious spec-manship, filling in some obvious gaps. We had a version of this in chromium a few years ago. This is a proposal just picking that up from what I can tell.
- theresistor 9y agoBitcast, including the specific nomenclature, comes from LLVM, which had the concept more than 10 years ago.
- gpderetta 9y agoThe concept of bitcasting is as old as programming. In the context of C++, I'm also pretty sure that the bitcast template, in one form of another, is also much older than LLVM itself.