From db04827bccdfda03352bc211619791c62a537a15 Mon Sep 17 00:00:00 2001 From: Ben Deane Date: Fri, 11 Sep 2026 08:05:09 -0600 Subject: [PATCH] :art: 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. --- docs/bitset.adoc | 2 ++ include/stdx/bitset.hpp | 5 +++++ test/bitset.cpp | 6 ++++++ 3 files changed, 13 insertions(+) diff --git a/docs/bitset.adoc b/docs/bitset.adoc index 7a96248..8a79a48 100644 --- a/docs/bitset.adoc +++ b/docs/bitset.adoc @@ -22,6 +22,8 @@ platform. * `size` does the same as `count()`: `capacity()` is the static size * `empty` (size == 0) is provided * `clear` does the same as `reset` +* The single-argument constructor (from `std::uint64_t`) is `explicit`. There is + also an assignment operator from `std::uint64_t`. A bitset has two template parameters: the size of the bitset and the storage element type to use. The storage element type must be unsigned. diff --git a/include/stdx/bitset.hpp b/include/stdx/bitset.hpp index 1f780e2..34c8acd 100644 --- a/include/stdx/bitset.hpp +++ b/include/stdx/bitset.hpp @@ -170,6 +170,11 @@ class bitset { } } + constexpr auto operator=(std::uint64_t value) -> bitset & { + *this = bitset{value}; + return *this; + } + template constexpr explicit bitset(place_bits_t, Bs... bs) { static_assert(((std::is_integral_v or std::is_enum_v) and ...), diff --git a/test/bitset.cpp b/test/bitset.cpp index 9864606..2191bd2 100644 --- a/test/bitset.cpp +++ b/test/bitset.cpp @@ -575,3 +575,9 @@ TEST_CASE("fix type of bitset argument to std::size_t", "[bitset]") { STATIC_CHECK(std::same_as); STATIC_CHECK(std::same_as>); } + +TEST_CASE("bitset assign", "[bitset]") { + auto bs = stdx::bitset<16>{}; + bs = 0xa5a5; + CHECK(bs.to_natural() == 0xa5a5); +}