Skip to content

Commit 363928c

Browse files
committed
✨ Allow bitset construction with enums for place_bits
Problem: - It is convenient to construct a bitset with bits that are identified with enum values. Solution: - Allow the `place_bits` constructor to take enumeration values.
1 parent ec147d5 commit 363928c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

include/stdx/bitset.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ class bitset {
135135

136136
template <typename... Bs>
137137
constexpr explicit bitset(place_bits_t, Bs... bs) {
138-
static_assert((std::is_integral_v<Bs> and ...),
139-
"Bit places must be integral!");
138+
static_assert(((std::is_integral_v<Bs> or std::is_enum_v<Bs>) and ...),
139+
"Bit places must be integral or enumeration types!");
140140
(set(static_cast<std::size_t>(bs)), ...);
141141
}
142142

test/bitset.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ namespace {
427427
enum struct Bits : std::uint8_t { ZERO, ONE, TWO, THREE, MAX };
428428
}
429429

430-
TEST_CASE("use bitset with enum struct (construct)", "[bitset]") {
430+
TEST_CASE("use bitset with enum struct (default construct)", "[bitset]") {
431431
constexpr auto bs = stdx::bitset<Bits::MAX>{};
432432
static_assert(bs.size() == stdx::to_underlying(Bits::MAX));
433433
}
@@ -455,6 +455,11 @@ TEST_CASE("use bitset with enum struct (read index)", "[bitset]") {
455455
static_assert(bs[Bits::THREE]);
456456
}
457457

458+
TEST_CASE("use bitset with enum struct (place_bits construct)", "[bitset]") {
459+
constexpr auto bs = stdx::bitset<Bits::MAX>{stdx::place_bits, Bits::ZERO};
460+
static_assert(bs.to_natural() == 1);
461+
}
462+
458463
#if __cplusplus >= 202002L
459464
TEST_CASE("construct with a ct_string", "[bitset]") {
460465
using namespace stdx::literals;

0 commit comments

Comments
 (0)