<div dir="ltr"><div dir="auto"><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Sep 13, 2021, 2:02 AM Serge Pavlov via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">The working construct is `reinterpret_cast<uint32_t&>(x)`. It however possesses the same drawback, it requires `x` be in memory.</div></blockquote></div></div><div dir="auto"><br></div><div dir="auto">We're getting rather far afield of the thread topic here, but .. that is UB, don't do that.</div><div dir="auto"><br></div><div dir="auto">Instead, always memcpy, e.g.</div><div dir="auto">uint32_t y;</div><div dir="auto">memcpy(&y, &flo, sizeof(uint32_t));</div><div dir="auto"><br></div><div dir="auto">Or use a wrapper like std::bit_cast or absl::bit_cast (<a href="https://github.com/abseil/abseil-cpp/blob/cfbf5bf948a2656bda7ddab59d3bcb29595c144c/absl/base/casts.h#L106" target="_blank">https://github.com/abseil/abseil-cpp/blob/cfbf5bf948a2656bda7ddab59d3bcb29595c144c/absl/base/casts.h#L106</a>). </div><div dir="auto"><br></div><div dir="auto">This has effectively no runtime overhead, the compiler is extremely good at deleting calls to memcpy when it has a constant smallish size. And remember that <i>every</i> local variable started out in memory. Only through optimizations does the memory location and the loads/stores for every access get eliminated.</div><div dir="auto"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
</blockquote></div></div></div>
</div>