r/cpp • u/alberto-m-dev • Jun 26 '26
Improvements to std::format in C++26
https://mariusbancila.ro/blog/2026/06/19/improvements-to-stdformat-in-c26/
91
Upvotes
r/cpp • u/alberto-m-dev • Jun 26 '26
View all comments
Show parent comment
12
u/fdwr fdwr@github 🔍 Jun 27 '26 edited Jun 27 '26
Oof, so it's Python's fault. I'm really glad you contributed
fmtandstd::formatVictor 👏🙏, and overall I lovestd::format💚, but hex formatting is one aspect where I wish Python's conventions (Python not being a low-level language that deals with raw bit representations and stack traces) had been secondary to consistency with existing C++ precedent, withint x = -42preferably displaying asFFFFFFD6which matchesprintf("%08X", x)andstd::cout << std::hex << std::uppercase << xandstd::to_chars(std::begin(buffer), std::end(buffer), x, 16);, rather than-000002A.Maybe there is a use case for
-2Atoo 🤷♂️, but having a compatible successor formatting option so we can more directly replace our oldsprintfandStringCchPrintfcalls would be prudent (shoot, now I should revisit some recent code changes to our buffer format display logic to ensure we didn't regress anything 😯, as I see that binary display also does the same-absoluteValuebehavior...). Until then, I'll be careful about ever usingstd::formatwith signed integers in hex or binary, and as for pointers, I'll continue using the more standard notation0xABC123via0x{:08X}(leaving{:P}to disuse 😅).🤔💡 You know, there are still plenty of letters in the alphabet left to use - maybe we can still get a more typical two's complement hex 😉 and a pointer syntax that doesn't prepend the prefix automatically so we can say
std::print("0x{:Q}", p)and get0x0004AB08?...