Semi-disabled bitFlagsToInt() again, since it is quite error-prone.

This commit is contained in:
Patrick Wuttke 2025-03-27 15:07:45 +01:00
parent 8bad5e4346
commit 91d53805b5

View File

@ -29,6 +29,7 @@ template<typename TBits>
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<T>;
template<std::integral TInt, BitFlagsType T>
TInt bitFlagsToInt(const BitFlags<T>& 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;