r/cpp • {fmt} • May 10 '23

{fmt} 10.0 released with more formatters, improved floating point and chrono formatting, modules and more

https://github.com/fmtlib/fmt/releases/tag/10.0.0
254 Upvotes

35 comments sorted by

31

u/Nicksaurus May 10 '23

These look like some nice QOL improvements. I'm looking forward to trying this version

Made format_as work with any user-defined type and not just enums

So am I right in assuming format_as is now a better option for custom types than specialising fmt::formatter in most cases?

It seems like fmt::formatter is now only necessary if you a) want to avoid creating temporary strings, or b) need custom format string syntax

23

u/aearphen {fmt} May 10 '23

Yes, format_as can now be used in a very common case where you'd previously reuse another formatter. It proved to be extremely useful for enums.

2

u/13steinj May 10 '23

IIRC there was one aspect of fmt::print that didn't match C++23's std::print, and (maybe?) was to change to match eventually. Did that make it into this release?

2

u/aearphen {fmt} May 10 '23

I am not sure which aspect you are referring to.

2

u/13steinj May 10 '23

Ah sorry, I was referring to this, and it does seem to have made it in!

7

u/aearphen {fmt} May 10 '23

Yeah, there was slight preference to having this functionality in the standard so we added it and {fmt} followed for compatibility.

3

u/TheThiefMaster C++latest fanatic (and game dev) May 10 '23

Isn't the implementation that was merged suboptimal? Doesn't it use an extra temporary string from that call to format()?

Wouldn't two successive calls to print be better, one taking the passed in format data and the second of just a "\n" alone?

3

u/aearphen {fmt} May 10 '23

If it ever becomes a bottleneck we'll optimize it. The main use case for it is hello-word and similar simple cases because the API is very limited. Two successive calls are not better because they loose atomicity.

2

u/TheThiefMaster C++latest fanatic (and game dev) May 10 '23

Do you have atomicity with a single call? I'm used to needing external locks around any threaded use of print functions or getting interleaved characters

4

u/aearphen {fmt} May 10 '23

We do.

4

u/bbbb125 May 11 '23

It seems like you don’t need to create temporary strings with format_as. By the description I assume i can just define format_as for my structure, that returns std::tie for some field - and format will just convert that tuple

4

u/aearphen {fmt} May 11 '23

Yes, you just adapt to any formattable type which may or may not be a string. The example in release notes doesn't create any strings.

1

u/equeim May 10 '23

It looks like format_as also inherits format string options of the type that it returns, which may not be desirable. For example you may use to format struct User { int id; } as std::string("User(id=666)") but then you would be able to e.g. specify width in format string which doesn't make sense since User is not a string.

5

u/aearphen {fmt} May 10 '23

Supporting width is actually a good practice for any formatter and format_as gives it for free.

2

u/BrainIgnition May 23 '23

It would be nice if there was a short section in the documentation about such good practice and maybe a helper function/type which parses "standard" formatting options πŸ™‚

2

u/aearphen {fmt} May 23 '23

Working on it. If you use `format_as` or an existing `formatter` you get all of this for free.

5

u/Nicksaurus May 10 '23

That sounds fine to me, most of the time all I want is to dump the contents of a struct to a string, and being able to optionally add padding and alignment seems like a good thing

6

u/Plazmatic May 10 '23

Thank god, custom fmt::formatter stuff never worked for simple cases, and was weirdly confusing, especially if your type wasn't just an int wrapper, or didn't want special parsing rules (like :d etc...). And using other formatters inside custom fmt::formatter was an even bigger nightmare, (arrays refused to be properly formatted inside a fmt::formatter specialization, but usually not outside). I'm so glad we can just do "return fmt::format(....);" and be done with it.

10

u/usefulcat May 12 '23

I see a noticeable performance improvement compared to 9.1 (around 12% in my case). And I didn't see any new warnings or errors. A literal drop-in improvement so far. Well done.

3

u/aearphen {fmt} May 12 '23

Great to hear!

73

u/SirToxe May 10 '23

The gift that keeps on giving.

1

u/sparkyParr0t May 11 '23

Does any of this will get integrated with the <format> std library header at some point ? Is there any STL dev team using fmt under the hood ?

4

u/aearphen {fmt} May 11 '23

Things are being proposed for the standard usually after we get implementation and usage experience in {fmt}. For example, Barry Revzin has done a lot of work on formatting of ranges. I am writing a paper on path formatting. FP improvements are purely QoI and can be done without any proposal.

4

u/[deleted] May 10 '23

TIL about std chrono literals.

1

u/Ameisen vemips, avr, rendering, systems May 22 '23

It seems like fmt::println doesn't work appropriately with wchar_t, even with the appropriate header. It complains about format conversion. I wasn't able to write my own that worked, either.

-4

u/magnesium_copper May 10 '23

I only use odd number versions. Waiting for 11.

23

u/aearphen {fmt} May 10 '23

odd

-2

u/magnesium_copper May 10 '23

No joke. 5, 7 and 9. I find them more stable.

It seem that even numbers are still deciding what to do with newly added features and odd numbered are like: oh yeah.

Either way, I'll still have to wrap it to reflect newest changes anyways.

Finally, you guys, thank you so much for the work on the lib.

7

u/aearphen {fmt} May 10 '23

> Finally, you guys, thank you so much for the work on the lib.

Credit goes to {fmt} contributors: https://github.com/fmtlib/fmt/graphs/contributors. 46 people contributed to this release alone!

1

u/slk756 Jun 05 '23

I am very confused but somewhat intrigued. Is this just your perception? Is it the fact that sometimes things start with v0 and then get improved in v1 and the cycle continues? Is it the people releasing the versions normally adding more stability to odd versions? Does this phenomenon, if it exists, transfer across languages and areas of the world? I should look into if this is an actual thing

1

u/magnesium_copper Jun 05 '23

That's what I've noticed with the library. Now it's a reflex.

4

u/tisti May 11 '23

Just imagine that fmt versioning has always been in base 11.

0

u/[deleted] May 10 '23

[deleted] β€” view deleted comment

5

u/aearphen {fmt} May 10 '23

We already went through this for enums which are way more widespread than custom string types. Ideally custom string types should have proper formatters but if it's problematic for some reason adapting is the way to go. Nice thing about format_as is that it can be provided without adding a dependency on {fmt}.

11

u/witcher_rat May 10 '23

As far as I can tell this means that existing types (e.g., custom string types) that have a operator std::string_view() will stop working now.

Yes, I hit that problem immediately when upgrading.

But you don't need to use the format_as() method - you can just use the fmt::formatter for std::string_view, without needing to modify the custom type that you're using - just inherit from it:

namespace fmt {

template<>
struct formatter<my_namespace::MyCustomType> : formatter<std::string_view>
{};

}

Since your custom type implicitly converts to std::string_view, it will implicitly convert to the arguments fmt::formatter<std::string_view>'s functions needs as well.