-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[348] - Did you know that C++26 added more constexpr for
<cmath>
an…
…d `<complex>`?
- Loading branch information
1 parent
3eb0f4c
commit 8f653f8
Showing
3 changed files
with
100 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<details open><summary>Info</summary><p> | ||
|
||
* **Did you know that C++26 added testing for success or failure of <charconv> functions?** | ||
|
||
* https://wg21.link/P2497 | ||
|
||
</p></details><details open><summary>Example</summary><p> | ||
|
||
```cpp | ||
constexpr std::to_chars_result result{{}}; | ||
static_assert(result); | ||
``` | ||
> https://godbolt.org/z/q16djPn3P | ||
</p></details><details open><summary>Puzzle</summary><p> | ||
* **Can you implement `format` fn which optionally converts given value to chars?** | ||
```cpp | ||
template <auto N> | ||
constexpr auto format(const auto value) -> std::optional<std::array<char, N>>; // TODO | ||
constexpr auto fmt_0 = format<1>(0); | ||
static_assert(fmt_0 and std::string_view{fmt_0->cbegin(), fmt_0->cend()} == std::string_view{"0"}); | ||
constexpr auto fmt_42 = format<2>(42); | ||
static_assert(fmt_42 and std::string_view{fmt_42->cbegin(), fmt_42->cend()} == std::string_view{"42"}); | ||
``` | ||
|
||
> https://godbolt.org/z/rf7rWc3ee | ||
</p></details> | ||
|
||
</p></details><details><summary>Solutions</summary><p> | ||
|
||
```cpp | ||
template <auto N> | ||
constexpr auto format(const auto value) -> std::optional<std::array<char, N>> { | ||
std::array<char, N> buffer; | ||
return std::to_chars(buffer.begin(), buffer.end(), value) | ||
? std::optional(buffer) | ||
: std::nullopt; | ||
} | ||
``` | ||
> https://godbolt.org/z/c8vWbGKWf | ||
```cpp | ||
template <auto N> | ||
constexpr auto format(const auto value) -> std::optional<std::array<char, N>> { | ||
std::array<char, N> result{}; | ||
if (not std::to_chars(result.begin(), result.end(), value)) { | ||
return std::nullopt; | ||
} | ||
return result; | ||
} | ||
``` | ||
|
||
> https://godbolt.org/z/nrWcq4rz5 | ||
</p></details> |
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,37 @@ | ||
<details open><summary>Info</summary><p> | ||
|
||
*** **Did you know that C++26 added more constexpr for <cmath> and <complex>?** | ||
|
||
* https://wg21.link/P1383 | ||
|
||
</p></details><details open><summary>Example</summary><p> | ||
|
||
```cpp | ||
#include <cmath> | ||
constexpr auto positive = std::abs(-2); | ||
static_assert(positive == 2); | ||
``` | ||
> https://godbolt.org/z/ar1drdohP | ||
</p></details><details open><summary>Puzzle</summary><p> | ||
* **Can you implement `number` user defined literal (UDL) which uses constexpr <cmath> functions?** | ||
```cpp | ||
#include <cmath> | ||
template <char... Cs> | ||
[[nodiscard]] constexpr auto operator""_number(); // TODO | ||
static_assert(0 == 0_number); | ||
static_assert(42 == 42_number); | ||
static_assert(123 == 123_number); | ||
``` | ||
|
||
> https://godbolt.org/z/8r4frajnv | ||
</p></details> | ||
|
||
</p></details><details><summary>Solutions</summary><p> | ||
</p></details> |