Skip to content

Commit

Permalink
[348] - Did you know that C++26 added more constexpr for <cmath> an…
Browse files Browse the repository at this point in the history
…d `<complex>`?
  • Loading branch information
krzysztof-jusiak committed Sep 10, 2023
1 parent 3eb0f4c commit 8f653f8
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Tips

* [[347]](/tips/347.md) - Did you know that C++26 added more constexpr for `<cmath>` and `<complex>`?
* [[346]](/tips/346.md) - Did you know that C++26 added testing for success or failure of `<charconv>` functions?
* [[345]](/tips/345.md) - Did you know that C++26 allows constexpr cast from `void*`?
* [[344]](/tips/344.md) - Did you know that C++26 added `Member visit`?
Expand Down
62 changes: 62 additions & 0 deletions tips/347.md
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>
37 changes: 37 additions & 0 deletions tips/348.md
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>

0 comments on commit 8f653f8

Please sign in to comment.