#pragma once #include #include #include namespace ext { /** \brief Returns value `from` converted to type `To` while retaining bit representation. * `To` and `From` must satisfy `CopyConstructible`. */ template std::decay_t bit_cast(const From & from) { To res {}; memcpy(static_cast(&res), &from, std::min(sizeof(res), sizeof(from))); return res; } /** \brief Returns value `from` converted to type `To` while retaining bit representation. * `To` and `From` must satisfy `CopyConstructible`. */ template std::decay_t safe_bit_cast(const From & from) { static_assert(sizeof(To) == sizeof(From), "bit cast on types of different width"); return bit_cast(from); } }