Skip to content

Commit

Permalink
[348] - Did you know that C++26 changed arithmetic overloads of std::…
Browse files Browse the repository at this point in the history
…to_string and std::to_wstring to use std::format?
  • Loading branch information
kris-jusiak committed Sep 17, 2023
1 parent 57309b2 commit b4f3dcf
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Tips

* [[348]](/tips/348.md) - Did you know that C++26 changed arithmetic overloads of std::to_string and std::to_wstring to use std::format?
* [[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*`?
Expand Down Expand Up @@ -124,7 +125,7 @@
* [[238]](/tips/238.md) - Did you know that Circle Meta-model allows for applying `normal` STL for operations on @meta types?
* [[237]](/tips/237.md) - Did you know about C++2X proposal for the Circle Meta-model for compilation-time meta-progr
* [[236]](/tips/236.md) - Did you know about `__builtin_dump_struct` clang-extension which can nicely print a struct?
* [[235]](/tips/235.md) - Did you know that C++20 `[[no_unique_address]]` can be used to implement lazy/fast/memory efficient
* [[235]](/tips/235.md) - Did you know that C++20 `[[no_unique_address]]` can be used to implement lazy/fast/memory efficient
* [[234]](/tips/234.md) - Did you know about function-try-block and that exceptions caught inside that block are implicitly
* [[233]](/tips/233.md) - Did you know that C++20 made `typename` more optional?
* [[232]](/tips/232.md) - Did you know that different overloads can have different specifiers?
Expand Down
1 change: 0 additions & 1 deletion feed.template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
<!-- START FEED -->
</channel>
</rss>

26 changes: 26 additions & 0 deletions tips/347.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,31 @@ static_assert(123 == 123_number);
</p></details>

</p></details><details><summary>Solutions</summary><p>

```cpp
template <char... Cs>
[[nodiscard]] constexpr auto operator""_number() {
return []<auto... Is, class T = int>(std::index_sequence<Is...>) {
return std::integral_constant<
T, (((Cs - '0') * T(std::pow(T(10), sizeof...(Is) - Is - 1))) +
...)>{};
}
(std::make_index_sequence<sizeof...(Cs)>{});
}
```

> https://godbolt.org/z/qMvKcc8ff

```cpp
template <char... Cs>
[[nodiscard]] constexpr auto operator""_number() {
int result = 0;
((result = result * 10 + (Cs - '0')), ...);
return result;
}
```

> https://godbolt.org/z/7vs1d51no
</p></details>
46 changes: 46 additions & 0 deletions tips/348.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<details open><summary>Info</summary><p>

* **Did you know that C++26 changed arithmetic overloads of std::to_string and std::to_wstring to use std::format?**

* https://wg21.link/P2587

</p></details><details open><summary>Example</summary><p>

```cpp
int main() {
setlocale(LC_ALL, "C");
std::cout << std::to_string(42); // prints 42
std::cout << std::to_string(.42); // prints 0.42
std::cout << std::to_string(-1e7); // prints -1e+07
}
```

> https://godbolt.org/z/a7xMEq336
</p></details><details open><summary>Puzzle</summary><p>

* **Can you add required `locale` to match expectations?**

```cpp
int main() {
using namespace boost::ut;
using std::literals::string_literals::operator""s;

"locale.to_string (all.us)"_test = [] {
// TODO
expect("-1e+07"s == std::to_string(-1e7));
};

"locale.to_string (numeric.eu)"_test = [] {
// TODO
expect("1,23"s == std::to_string(1.23));
};
}
```

> https://godbolt.org/z/jocqoM7xd
</p></details>

</p></details><details><summary>Solutions</summary><p>
</p></details>

0 comments on commit b4f3dcf

Please sign in to comment.