r/cpp • u/aearphen {fmt} • Dec 02 '19
{fmt} 6.1 released with improved floating-point formatting, locale support, faster compile times and more
https://github.com/fmtlib/fmt/releases/tag/6.1.011
u/boredcircuits Dec 02 '19
Awesome work!
I'm curious, what's the rationale behind this change?
Changed formatting of octal zero with prefix from "0o0" to "0":
13
u/aearphen {fmt} Dec 02 '19
Thanks! The rationale is consistency with C++20 `std::format` and more generally with the syntax of octal numbers in C++. `0o` is a Pythonism and doesn't mean anything in C++.
8
u/sahsahaha Dec 02 '19
Huh?
Isn't it just common sense that if 0x is for hex, then surely...
No, I didn't expect familiar things to have familiar prefixes or anything at all.
23
u/aearphen {fmt} Dec 02 '19
Personally I'd prefer if C and C++ used `0o` for octal but that ship has sailed long time ago =).
1
u/nintendiator2 Dec 04 '19
This has always irked me about C and C++. It makes it a pita to format numbers in arrays and stuff like that nicely, for when you are splitting off numbers in source code with thousands separators and the like.
Then again, I guess it better stays at it is. If the Committee attempt to fix it they are likely to force all decimal literals to be
0a....9
Dec 02 '19
[removed] — view removed comment
8
Dec 02 '19
fun fact, all zero literals are octal as they start with zero
11
-2
Dec 03 '19
[removed] — view removed comment
6
Dec 03 '19
the literal doesn't reflect the representation, if i understand correctly. So whether you put 0x10, 16, 0b10000, or 020 they are all represented the same in memory. So 6 + 0x10 is the same as 6 + 16
1
u/mujjingun Dec 03 '19
no, 0+6 is decimal 6 because octal 0 + decimal 6 = decimal 6.
0
Dec 03 '19
[removed] — view removed comment
5
u/mujjingun Dec 03 '19
There is no rule. Once the numbers get interpreted as a certain base, there is no base anymore, they are just numbers. Octal 0 = zero, decimal 6 = six. zero plus six = six.
1
u/sahsahaha Dec 03 '19
The issue is that I simply didn't know as I never really needed octal for anything.
And then, my question kind of becomes... Why?
Was octal introduced before hexadecimal?
4
u/boredcircuits Dec 03 '19
I get that much, bit the description here implies that
0ois still used for other numbers besides 0. So 8 is printed as0o10, but 0 is printed as0instead of0o0. That's bizarre behavior.But maybe I'm reading to much into that description, and it's using the unfortunate 0 prefix across the board for octal, so 8 prints as
010? That's more understandable, at least. And consistent.I do hope scnlib or whatever it's called allows for parsing both types of prefixes. Leading zeros turning decimal numbers to octal was a mistake. But it's less of a problem with printing, as you can just make up your own prefix if you need.
5
u/aearphen {fmt} Dec 03 '19
Actually there is a mistake in the changelog. The change was from `00` to `0`, not from `0o0` to `0`. I'll fix the description.
4
4
u/Bart_V Dec 03 '19
Great! What are the implications for std::format in C++20? Will it get the update too?
5
u/aearphen {fmt} Dec 03 '19
There are no implications for C++20
std::format. Changes to floating-point and octal zero formatting make {fmt} closer follow the standard.
6
u/simonask_ Dec 03 '19
std::locale is notoriously broken, and tends to cause severe detriment to any codebase that uses it. Is fmt's locale support opt-in?
8
u/aearphen {fmt} Dec 03 '19
It is an opt-in in the sense that all formatting is locale-independent by default and the user has to explicitly request locale-specific formatting if required. It is also possible to compile {fmt} completely without locale support which is useful for embedded systems.
4
u/simonask_ Dec 03 '19
And, I would argue, for all systems everywhere. :-)
std::localeshould be avoided at all costs. It is almost never what you actually want.4
u/alexej_harm Dec 03 '19
Look at the examples - yes it is.
6
u/simonask_ Dec 03 '19
Just because passing an
std::localeto the function is optional, that doesn't mean it isn't used internally. :-)
3
u/TrueTom Dec 03 '19
I wish there would be an easier way to add custom types.
4
u/aearphen {fmt} Dec 03 '19
Normally you can delegate all the work to existing formatter specializations.
2
u/SausageTaste Dec 06 '19
I don't need python for small text manipulating scripts anymore thanks to {fmt}. It just made C++ more friendly for beginners. Thanks!
3
1
u/shadedtriangle Dec 03 '19
Thank you for making {fmt} and for having locale support built in!
I was curious, does {fmt} support the inverse operation for parsing structured data (i.e. scanf to printf)? Could I do say:
fmt::parse(input, "{}, {}, {}", x, y, z);To parse structured csv data for example?
3
21
u/liquidify Dec 02 '19
awesome work !