From 7951befa5d0af0abcb54d151e4c11f7556200392 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Wed, 4 Feb 2026 12:08:33 -0700 Subject: [PATCH] :bug: Fix bitset construction Problem: - When a `bitset` has zero size, it cannot be constructed with `all_bits`. Solution: - Check for zero size in fixing up the end bits. --- include/stdx/bitset.hpp | 4 +++- test/bitset.cpp | 2 ++ test/type_bitset.cpp | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/stdx/bitset.hpp b/include/stdx/bitset.hpp index 7df348c..04bfc0f 100644 --- a/include/stdx/bitset.hpp +++ b/include/stdx/bitset.hpp @@ -175,7 +175,9 @@ class bitset { for (auto &elem : storage) { elem = allbits; } - storage.back() &= lastmask; + if constexpr (N > 0) { + storage.back() &= lastmask; + } } constexpr explicit bitset(std::string_view str, std::size_t pos = 0, diff --git a/test/bitset.cpp b/test/bitset.cpp index e3485ee..065caef 100644 --- a/test/bitset.cpp +++ b/test/bitset.cpp @@ -516,4 +516,6 @@ TEST_CASE("zero size bitset", "[bitset]") { CHECK(bs3.to() == 0); bs3 >>= 1; CHECK(bs3.to() == 0); + bs3 = stdx::bitset<0>{stdx::all_bits}; + CHECK(bs3.to() == 0); } diff --git a/test/type_bitset.cpp b/test/type_bitset.cpp index 1b52453..adc774c 100644 --- a/test/type_bitset.cpp +++ b/test/type_bitset.cpp @@ -10,6 +10,7 @@ #include TEST_CASE("bitset size", "[type_bitset]") { + STATIC_CHECK(stdx::type_bitset<>{}.size() == 0u); STATIC_CHECK(stdx::type_bitset{}.size() == 1u); STATIC_CHECK(stdx::type_bitset{}.size() == 2u); }