Skip to content

Commit

Permalink
343 - Did you know that C++26 std.format added formatting pointers ab…
Browse files Browse the repository at this point in the history
…ility?
  • Loading branch information
kris-jusiak committed Aug 13, 2023
1 parent a7da544 commit 069ed06
Show file tree
Hide file tree
Showing 3 changed files with 67 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

* [[343]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/343.md) - Did you know that C++26 std.format added formatting pointers ability?
* [[342]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/342.md) - Did you know that C++26 added 'A nice placeholder with no name'?
* [[341]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/341.md) - Did you know that C++26 added user-generated static_assert messages?
* [[340]](https://github.com/QuantlabFinancial/cpp_tip_of_the_week/blob/master/tips/340.md) - Did you know that C++26 added bind front and back to NTTP callables?
Expand Down
23 changes: 23 additions & 0 deletions tips/342.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,27 @@ int main() {

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

```cpp
auto guard() {
std::mutex m;
std::lock_guard _{m};
// ...
}

auto structure_bindigns() {
auto [a, _, c] = std::tuple{1, 2, 3};
(void)a;
(void)c;
}

auto assert_() {
auto _ = 42;
assert(_ == 42);
}

template<auto _> auto nttp() {}
```

> https://godbolt.org/z/dx9691YKn
</p></details>
43 changes: 43 additions & 0 deletions tips/343.md
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++26 std.format added formatting pointers ability?

* https://wg21.link/P2510

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

```cpp
int main() {
auto i = 42;
std::cout << std::format("{:#018X}", reinterpret_cast<uintptr_t>(&i)); // prints 0X00007FFD9D71776C
}
```

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

* **Can you fill format strings to properly format given pointers?**

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

"std.format ptr"_test = [] {
auto ptr = reinterpret_cast<std::uintptr_t>(nullptr);

expect("0"sv == std::format("TODO", ptr));
expect("000000000000000000"sv == std::format("TODO", ptr));
expect("0x0000000000000000"sv == std::format("TODO", ptr));
expect("0X0000000000000000"sv == std::format("TODO", ptr));
};
}
```

> https://godbolt.org/z/4eqznEh4q
</p></details>

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

0 comments on commit 069ed06

Please sign in to comment.