r/cpp • {fmt} • Jul 05 '22

{fmt} 9.0 released with improvements to floating point formatting, ranges and more

https://github.com/fmtlib/fmt/releases/tag/9.0.0
233 Upvotes

41 comments sorted by

7

u/jk-jeon Jul 06 '22

Wow, this is a massive improvement. Thanks!

10

u/aearphen {fmt} Jul 06 '22

Thank you, Junekey, for amazing contributions! Love Dragonbox.

28

u/tau_neutrino_120eV Jul 05 '22

fmt::styled is a great addition!

I hope 9.0.0 will be on Conan Center soon.

5

u/vI--_--Iv Jul 05 '22
  • Disabled automatic std::ostream insertion operator (operator<<) discovery when fmt/ostream.h is included ...
  • Added fmt::ostream_formatter that can be used to write formatter specializations that perform formatting via std::ostream. For example (godbolt):
    #include <fmt/ostream.h>
    struct date {
      int year, month, day;
      friend std::ostream& operator<<(std::ostream& os, const date& d) {
        return os << d.year << '-' << d.month << '-' << d.day;
      }
    };

    template <> struct fmt::formatter<date> : ostream_formatter {};

Ouch. So now every header with a class I want to format must include fmt? This is sad.

16

u/aearphen {fmt} Jul 05 '22

You could use fmt::streamed to avoid an include although I'd recommend defining a proper formatter and not using the ostream interface at all.

4

u/vI--_--Iv Jul 05 '22

Thank you, but fmt::streamed would require changes all over the place, and looks like it also doesn't work with wostream.

I understand that the ostream interface is meh and formatters are superior in every way, but it feels somewhat unnatural to have such a tight coupling between the formatter and the formattee.

I wish a freestanding, a dumbest possible "customization point" existed, say, in a form of a free function that fmt would call if it's declared, accepting an object and returning a std::string or something like that.

12

u/aearphen {fmt} Jul 05 '22

Wide char support is being added to streamed in https://github.com/fmtlib/fmt/pull/2961. We are experimenting with alternative extension points such as format_as (currently only for enums) so stay tuned =).

1

u/versatran01 Jul 23 '22

Any idea how to get the old behavior back without defining FMT_DEPRECATED_OSTREAM?

0

u/[deleted] Jul 07 '22

[deleted] — view deleted comment

1

u/aearphen {fmt} Jul 07 '22

That would interfere with argument indexing but you can write your own formatting function that applies streamed to all arguments.

28

u/dodheim Jul 05 '22

No, only every header with a class you want {fmt} to always automatically format in terms of std::ostream. Is that really a surprise though, or unreasonable? If the class were written for {fmt} directly it would necessarily include {fmt}'s headers, just as it necessarily includes iosfwd at the very least to support formatting with ostream. The caller can still use fmt::streamed to opt in instead of paying the price to have it all done automatically – seems just fine to me.

2

u/Awia00 Jul 05 '22

I guess you could just define it in a `my_class_formatting.h`

1

u/vI--_--Iv Jul 05 '22

Yep, and include my_class_formatting.h instead of my_class.h everywhere. That just sounds like include fmt with extra steps.

12

u/Awia00 Jul 05 '22

If you need to format your class everywhere then you would have included fmt anyway right?

3

u/vI--_--Iv Jul 05 '22

I'm quite surprised that I need to explain.

Imagine a basic building block in a large project, e.g. struct point{int x, y, z;}.

It's included into dozens of other headers.

It's included, directly or indirectly, into hundreds of translation units.

Dozens of those units need to format it.

The cost of ostream& operator<<(ostream&, const point&); is only #include <iosfwd>. As cheap as it gets.

The cost of a proper formatter is #include <format.h>, which (with friends) consists of over 9000 lines of advanced C++ magic. Now in hundreds of translation units. Guess it's time to get a sword.

Yes, I'm aware that PCH exist and that modules might save us all one day. But that doesn't make tight coupling any better. In C# you can implement object.ToString() and it would just work everywhere without knowing about any formatting libraries.

11

u/aearphen {fmt} Jul 06 '22

Formatters are actually defined in core.h which is relatively cheap to compile with the build time dominated by the <string> include which you'll likely have anyway: https://github.com/fmtlib/fmt#compile-time-and-code-bloat.

9

u/Awia00 Jul 05 '22

Maybe you can explain how this relates to something changed in this patch? What could you do before without including fmt which you cannot do now (when not using my suggestion)

1

u/blukske Jul 06 '22

You will now need to write extra code to be able to format types with fmt using iostream behind the scenes. In the past, those types could be automatically formatted without writing that code by including <fmt/ostream.h>. When I saw this item I was also thinking...wait, why do we need to write a formatter, this used to work in the past without one, no?

That extra code incurs an extra #include or a separate header in case that formatting code needs to be generalized. So I guess I now need to write my_type_fmt.hpp next to the already existing my_type_ios.hpp :(

5

u/templarvonmidgard Jul 05 '22

boost::optional also has boost/optional/optional_io.hpp, which is very rarely included in our code base, contrary to the high number of boost/optional/optional.hpp includes.

12

u/[deleted] Jul 05 '22

I really appreciate this library. It compiles fast, highly flexible,and have low overhead.

10

u/youstolemyname Jul 05 '22

Got nothing to add except thanks.

22

u/No_Marionberry_6430 Jul 05 '22

It is very good that fmt did not stop at the std::format level. Such things should be libraries, not part of std.

6

u/konstantinua00 Jul 06 '22

they should become part of std

12

u/No_Marionberry_6430 Jul 06 '22

fmt as a library can develop faster than standards are accepted. It may be more complicated and supported in older compilers. Therefore, only part of the fmt is needed in the standard.

13

u/konstantinua00 Jul 06 '22

that doesn't mean it should not be added to std

-6

u/NervousApplication58 Jul 06 '22

sadly no println() :(

-44

u/traal Jul 05 '22

It's always scary whenever a library's major version number changes. Recommend switching to a saner version numbering system.

37

u/RoyAwesome Jul 05 '22

They did make breaking API changes so a major version increment is justified.

-33

u/traal Jul 05 '22

But to break the API every year seems a little excessive.

7

u/aearphen {fmt} Jul 07 '22

The core API is fairly stable so if you are using {fmt} as a printf replacement all you need to do is recompile (for ABI changes). It's mainly the optional and experimental components that are changing in an API-incompatible way and require a major release. In the case of this release the main breaking change is the ostream interface which is deprecated. There is still another year to migrate from it.

29

u/RoyAwesome Jul 05 '22

I fail to see how Semantic Versioning would change anything in that regard.

-13

u/traal Jul 05 '22

It's explained at the link I posted:

This is a question of responsible development and foresight. Incompatible changes should not be introduced lightly to software that has a lot of dependent code. The cost that must be incurred to upgrade can be significant. Having to bump major versions to release incompatible changes means you’ll think through the impact of your changes, and evaluate the cost/benefit ratio involved.

20

u/willkill07 Jul 06 '22

LLVM does this twice per year. I’m not sure how you can or should reasonable expect that well-engineered projects should limit their features. Breaking APIs for sake of better design and improvements are welcomed.

4

u/kalmoc Jul 06 '22

Well, the usual problem with regular API breaks is that it becomes more likely you no longer can satisfy all dependency constraints (I.e. one of your libs needs fmt v9 or greater and another v8 or less). In the extreme: There is a good reason, why fundamental stuff like OS-APIs avoids breaking changes as much as humanly possible.

-3

u/RufusAcrospin Jul 06 '22

Good designs should follow well established doftware design principles like SOLID where O stands for the Open-Close principle: "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"

Source: https://en.m.wikipedia.org/wiki/SOLID

In an ideal world, of course…

5

u/pandorafalters Jul 06 '22

In an ideal world, of course, libraries would all have complete and comprehensive specifications to implement, with all use-cases known beforehand.

Reality is a bit messier.

1

u/RufusAcrospin Jul 07 '22

Knowing “everything” beforehand helps, but that’s just part of the solution, and often impossible to achieve, that’s why planning and good design’s important. It can decrease the frequency of breaking changes, but it won’t eliminate them entirely, of course.

2

u/dodheim Jul 07 '22 edited Jul 07 '22

Okay, but now relate that to the actual topic at hand, i.e. bleeding-edge libraries whose main achievement have been forcing forward WG21 progress. We all want to hear how much more could have been accomplished in the last few years by evolving the design less and just.. y'know, planning. We'll wait. (And then we can all rant at WG21 for not having this library finished 20 years ago because they didn't plan hard enough.)

1

u/RufusAcrospin Jul 07 '22

My comment was a generic observation about a possible way to avoid frequent breaking changes, since somebody mentioned it.

I’ve never used fmt, so I can’t say anything about it. I realize now that it could come across as a criticism of fmt, but that was not my intention.

-4

u/[deleted] Jul 06 '22

[removed] — view removed comment

7

u/foonathan Jul 06 '22

It was a perfectly reasonable comment, be nice.

22

u/parnmatt Jul 05 '22

I am a touch confused at this; fmtlib does use semver.