Example
int main() {
auto i = 42;
std::cout << std::format("{:#018X}", reinterpret_cast<uintptr_t>(&i)); // prints 0X00007FFD9D71776C
}
Puzzle
- Can you fill format strings to properly format given pointers?
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));
};
}
Solutions
"std.format ptr"_test = [] {
auto ptr = reinterpret_cast<std::uintptr_t>(nullptr);
expect("0"sv == std::format("{:#0}", ptr));
expect("000000000000000000"sv == std::format("{:#018}", ptr));
expect("0x0000000000000000"sv == std::format("{:#018x}", ptr));
expect("0X0000000000000000"sv == std::format("{:#018X}", ptr));
};