-
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.
343 - Did you know that C++26 std.format added formatting pointers ab…
…ility?
- Loading branch information
1 parent
a7da544
commit 069ed06
Showing
3 changed files
with
67 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
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++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> |