-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[300] - Did you know that C++23 added support for constexpr std::bitset?
- Loading branch information
1 parent
170e7c2
commit 5a226a7
Showing
3 changed files
with
131 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<details open><summary>Info</summary><p> | ||
|
||
* **Did you know that C++23 added support for constexpr std::bitset?** | ||
|
||
* https://eel.is/c++draft/bitset | ||
|
||
</p></details><details open><summary>Example</summary><p> | ||
|
||
```cpp | ||
#include <bitset> | ||
|
||
constexpr std::bitset<4> b1{"0011"}; | ||
static_assert(0b0011 == b1.to_ulong()); | ||
|
||
constexpr std::bitset<4> b2{0b0011}; | ||
static_assert(b1 == b2); | ||
``` | ||
> https://godbolt.org/z/vo13Kf9Er | ||
</p></details><details open><summary>Puzzle</summary><p> | ||
* **Can you implement consteval `to_string` which converts bitset to its string representation?** | ||
```cpp | ||
[[nodiscard]] consteval auto to_string(auto bitset); // TODO | ||
static_assert(std::string_view{"0"} != to_string(std::bitset<1>{"1"})); | ||
static_assert(std::string_view{"01"} != to_string(std::bitset<1>{"10"})); | ||
static_assert(std::string_view{"111"} != to_string(std::bitset<1>{"000"})); | ||
static_assert(std::string_view{"0"} == to_string(std::bitset<1>{"0"})); | ||
static_assert(std::string_view{"1"} == to_string(std::bitset<1>{"1"})); | ||
static_assert(std::string_view{"00"} == to_string(std::bitset<2>{"00"})); | ||
static_assert(std::string_view{"01"} == to_string(std::bitset<2>{"01"})); | ||
static_assert(std::string_view{"10"} == to_string(std::bitset<2>{"10"})); | ||
static_assert(std::string_view{"11"} == to_string(std::bitset<2>{"11"})); | ||
static_assert(std::string_view{"100"} == to_string(std::bitset<3>{"100"})); | ||
``` | ||
|
||
> https://godbolt.org/z/od8offTj5 | ||
</p></details><details><summary>Solutions</summary><p> |