diff --git a/source/mijin/util/bitflags.hpp b/source/mijin/util/bitflags.hpp index e34446a..e16a886 100644 --- a/source/mijin/util/bitflags.hpp +++ b/source/mijin/util/bitflags.hpp @@ -29,6 +29,7 @@ template struct BitFlags { using bits_t = TBits; + static constexpr bool ENABLE_TO_INT = false; constexpr TBits& operator |=(const BitFlags& other) { for (std::size_t idx = 0; idx < sizeof(TBits); ++idx) { @@ -103,6 +104,11 @@ concept BitFlagsType = is_bitflags_v; template TInt bitFlagsToInt(const BitFlags& flags) noexcept { + // If a BitFlags object contains padding (number of defined bits < number of bits in type), the bits in the padding might not be 0. + // In that case bit_casting will produce an incorrect value. + // Filling the gaps with padding bits fixes this, but is unfortunately quite error prone :/. + static_assert(T::ENABLE_TO_INT, "bitFlagsToInt not enabled for this type. Make sure the number of bits defined is the same as the number of bits in the type and define ENABLE_TO_INT to true."); + static constexpr std::size_t BYTES = std::min(sizeof(T), sizeof(TInt)); TInt result = 0;