Skip to content

Latest commit

 

History

History
57 lines (40 loc) · 1.45 KB

343.md

File metadata and controls

57 lines (40 loc) · 1.45 KB
Info

Example

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

https://godbolt.org/z/18rhfdT8x

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));
    };
}

https://godbolt.org/z/4eqznEh4q

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));
};

https://godbolt.org/z/seEahYP4T