Skip to content

Commit ffebde5

Browse files
committed
🎨 Add bitset::operator= from std::uint64_t
Problem: - It's annoying to have to write: ```cpp bs = decltype(bs){0b1010u}; ``` when you wanted to write: ```cpp bs = 0b1010u; ``` but can't because `bitset`'s single-argument constructor is explicit. Solution: - Add an assignment operator from `std::uint64_t` to match the constructor.
1 parent 2b9b63c commit ffebde5

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

‎docs/bitset.adoc‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ platform.
2222
* `size` does the same as `count()`: `capacity()` is the static size
2323
* `empty` (size == 0) is provided
2424
* `clear` does the same as `reset`
25+
* The single-argument constructor (from `std::uint64_t`) is `explicit`. There is
26+
also an assignment operator from `std::uint64_t`.
2527

2628
A bitset has two template parameters: the size of the bitset and the storage
2729
element type to use. The storage element type must be unsigned.

‎include/stdx/bitset.hpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ class bitset {
170170
}
171171
}
172172

173+
constexpr bitset(bitset const &) = default;
174+
constexpr bitset(bitset &&) = default;
175+
constexpr auto operator=(bitset const &) -> bitset & = default;
176+
constexpr auto operator=(bitset &&) -> bitset & = default;
177+
178+
constexpr auto operator=(std::uint64_t value) -> bitset & {
179+
*this = bitset{value};
180+
return *this;
181+
}
182+
173183
template <typename... Bs>
174184
constexpr explicit bitset(place_bits_t, Bs... bs) {
175185
static_assert(((std::is_integral_v<Bs> or std::is_enum_v<Bs>) and ...),

‎test/bitset.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,9 @@ TEST_CASE("fix type of bitset argument to std::size_t", "[bitset]") {
575575
STATIC_CHECK(std::same_as<A, B>);
576576
STATIC_CHECK(std::same_as<A, stdx::bitset<std::size_t{16}>>);
577577
}
578+
579+
TEST_CASE("bitset assign", "[bitset]") {
580+
auto bs = stdx::bitset<16>{};
581+
bs = 0xa5a5;
582+
CHECK(bs.to_natural() == 0xa5a5);
583+
}

0 commit comments

Comments
 (0)