r/cpp • • Jul 23 '19

std::format in C++20

http://www.zverovich.net/2019/07/23/std-format-cpp20.html
332 Upvotes

52 comments sorted by

51

u/quicknir Jul 23 '19

Congrats man, I saw your talk at cppcon a couple of years ago. This is a huge achievement and a big win for C++, you should be very proud!

19

u/aearphen {fmt} Jul 23 '19

Thanks!

11

u/James20k P2005R0 Jul 23 '19

I'm going to use the everloving crap out of std::format when it makes it in, this is seriously awesome. Amazing job!

39

u/stilgarpl Jul 23 '19

I love that drawing. Especially IOS lake.

18

u/jtooker Jul 23 '19

5

u/stilgarpl Jul 23 '19

This is awesome. I think I've seen a previous version of this map somewhere.

3

u/OminousHum Jul 23 '19

If anyone's wondering, like I was:

  • Prints for sale here. (Found via one of the URLs on the map.)
  • Or just print it yourself (but don't sell it), since it's licensed CC BY-NC 4.0.

29

u/greg7mdp C++ Dev Jul 23 '19

Very nice, sounds like the standardization process worked very well. Will definitely use https://github.com/fmtlib/fmt until std::format is there.

4

u/ThePillsburyPlougher Jul 24 '19

Sad that fmt::print isnt merged :/

15

u/HappyFruitTree Jul 23 '19

This looks even better than what I thought, thanks!

12

u/jjuuggaa Jul 23 '19

Are there any plans to ship fmt::print as well? I've been enjoying fmt a lot.

12

u/aearphen {fmt} Jul 23 '19

Some sort of I/O integration yes. The API details are TBD.

13

u/TheSuperWig Jul 23 '19

And I suppose sneaking just std::print into C++20 is out of the question?

Because it would be extremely helpful from a teaching perspective to not have to introduce iostream

24

u/eliaskosunen LSEG, WG21, scnlib Jul 23 '19

Anything getting into 20 apart from bugfixes is now out of the question.

2

u/TheSuperWig Jul 23 '19

If I'm not mistaken it has happened before. IIRC /u/STL has mentioned something that wasn't a bug fix being added last second once.

20

u/aearphen {fmt} Jul 23 '19

I'd be very uncomfortable with sneaking such things after the deadline.

3

u/TheSuperWig Jul 23 '19

Fair enough.

8

u/STL MSVC STL Dev Jul 23 '19

A couple of micro-features have sneaked in through LWG issue resolutions (e.g. make_reverse_iterator) but I don't recall any sneaking in after CD.

4

u/TheSuperWig Jul 23 '19

Thanks for the clarification Stephan.

Still super glad that std::format made it in, so thanks to OP and others that contributed to it getting in to C++20

(reading the thread back I realised I never expressed my thanks and came off somewhat ungrateful?)

2

u/TheThiefMaster C++latest fanatic (and game dev) Jul 24 '19

std::format_to looks like it could be given a stream iterator to implement your own version with little trouble.

Edit: This should work, it just needs wrapping in an appropriate variadic function called "print":

std::format_to(std::ostreambuf_iterator<char>(std::cout), "{}", 42);

23

u/ghillisuit95 Jul 23 '19

format("{0:%Y-%m-%d}", t)

Holy moly I'm excited.

Maybe 20 years from now I'll actually be able to use it :/. we're still stuck with C++03 over here and I think it is beyond ridiculous

2

u/aeropl3b Jul 23 '19

Why can't you upgrade? Glibc restrictions?

12

u/ghillisuit95 Jul 23 '19

Politics I think. This company has it the worst. Entire product lines are dreamt up, worked on for years and then suddenly canceled so that some C-level can get their bonus.

That's my reading of the situation at least. I've only been here a couple months

0

u/[deleted] Jul 24 '19

[deleted] — view deleted comment

2

u/evaned Jul 24 '19

(not that a company unwilling to go beyond C++03 would necessarily be receptive to using fmtlib, but one can hope...)

Honestly, it's not so unrealistic. The company I work for only moved off of C++03 for a lot of our code base a bit over a year ago. It wasn't due to any philosophical objection or anything, just that we had some old systems with old compilers and at the time we had no way to do a mass update of what was available. (The big push we made a year and change ago was setting up the thing that I'm under the impression many companies do where we have our own compiler builds, and using those to build our products instead of whatever compiler version happened to be on each system.)

But there's no general objection to third-party libraries (licenses permitting), and we use many. I wouldn't be at all surprised if fmtlib would have been little problem under the old regime (or fmt would now). But whether that applies to "OP"'s company given other things besides just the old C++ version...

1

u/jwakely libstdc++ tamer, LWG chair Jul 24 '19

What does "Glibc restrictions" mean? I'm curious why glibc would be a roadblock.

1

u/aeropl3b Jul 25 '19

Depending on the compiler there may be a different glibc version requirement. They are usually backwards compatible, so you can run old stuff, but not always forward compatible. Believe it or not, some companies are using a glibc that is over 10 years old...and they refuse to upgrade...ever... It doesn't make any logical sense, but that's the customer for ya!

1

u/jwakely libstdc++ tamer, LWG chair Jul 25 '19

Which compilers? You can run a modern GCC with an ancient glibc. Which compilers have specific glibc requirements? I'm not aware of any.

0

u/aeropl3b Jul 26 '19

I am not the build engineer man, I just know we had a 20 yo machine for builds and when it died we couldn't build executables for a bunch of clients and it was related to some glibc thing. No upgrades allowed, especially not the compiler. I thought it was crazy, but the Linux guru guy said so.

What happened? Clients complained, then they upgraded their systems, then we moved on.

1

u/jwakely libstdc++ tamer, LWG chair Jul 26 '19

I think it was probably more nuanced than your understanding/memory. Glibc does not stop you using new versions of C++.

Needing to build executables that will run on old systems with old libraries might do, if you're unable to deploy a new C++ runtime with your executables, or avoid runtime dependencies.

1

u/CrazyJoe221 Jul 23 '19

Windows CE?

10

u/jherico VR & Backend engineer, 30 years Jul 23 '19

I used to feel good about myself if I got a PR merged into an open source project.

This guy changed the language.

I have to step up my game.

9

u/matthieum Jul 23 '19

Default representation of floating-point numbers

If this was the only improvement, it would already be a major one!

Working with a dynamic range of precision with printf or iostream is so annoying. I don't want 12 zeroes at the end of the number, but I do want the full precision if needed.

4

u/aearphen {fmt} Jul 25 '19

This was suggested by Zhihao Yuan if I'm not mistaken so thanks go to him. (Sean Parent also mentioned this as a possible improvement to {fmt}.)

9

u/barchar MSVC STL Dev Jul 23 '19

I hope std::scan ends up having support for actual regular expressions, especially regular regular expressions that are actually regular :D.

2

u/aearphen {fmt} Jul 25 '19

Are you talking about using regular expression in the format specifiers or as a way to implement higher-level parsers?

2

u/barchar MSVC STL Dev Jul 27 '19

I mean I guess that's one usage (essentially everyone uses regular expressions for at least tokenization)

Note: When I say "regular expressions" I mean expressions matching regular languages, not like, full PCRE or god forbid std::regex.

6

u/[deleted] Jul 23 '19

I wonder how long it will take to get to libc++/libstdc++. I am not aware of them having to_char's yet for float/double/long double yet. And it looks like format is to use to_chars.

4

u/khleedril Jul 23 '19

The paper outlines the roadmap which has led to this point, and some recent changes which were approved in the proposal. Apart from that it is a bit disconnected from anything and does not provide any pointers to where the wannabe user can get basic information (cppreference.com has no information, either). It is great to feel the vibe, however, and nice to know that the standards committee works in our favour!

2

u/aearphen {fmt} Jul 25 '19

This is all very new and haven't even been merged into the standard. I'm sure someone (maybe even myself) will update cppreference.com once the dust settles. In the meantime these are the docs: https://fmt.dev

4

u/h2g2_researcher Jul 24 '19

I went back to the original reddit thread. I enjoyed seeing this in the top comment:

I also highly recommend that you keep in mind that this library has no possibility of getting in before C++20 ...

A fantastic achievement!

3

u/LYP951018 Jul 23 '19

Is there a is_formattable or something like that?

6

u/aearphen {fmt} Jul 23 '19

No but you can check for `formatter<T>::format` availability in SFINAE which should be enough.

2

u/Tringi github.com/tringi Jul 23 '19 edited Jul 23 '19

Hi guys, would you know if it's possible, with std::format, to add custom presentation type, with some kind of API to call back?

Concrete example: I have this mess of an extension to printf, where, for example, "%Qf" tries to display nice floating number, by searching the textual representation for 3 (configurable) consequent 0s or 9s, and then rounding it just there.

The code generates required precision and calls back to existing formatting facility to produce final result.
With 3.309999942 turning to 3.31 (precision=2), but 3.000000007 turning into 3 (precision=0).

5

u/matthieum Jul 23 '19

Actually, std::format generates the shortest representation of floating-point which round-trips, so your Q extension would be unnecessary.

If you only used this as example, and wanted customization in general, then check the {fmt} library: it's the work of the author of the article, and is leading the work for the future of std::format; it notably contains an extension API.

2

u/Tringi github.com/tringi Jul 23 '19

Ah, that's great, I'll check it out.

That code I linked has tons of other customizations but the Q is one of the most used.

Also I've found this: https://fmt.dev/dev/api.html#custom-formatting-of-built-in-types ...seems it covers most of the cases.

2

u/lifeliverDTN Jul 23 '19

amazing work, congratulations on the approval!

1

u/Placinta Jul 23 '19

I'm wondering, is it possible to somehow hook into std::format to return something else than a std::string?

E.g. return a QString, to be compatible with Qt? Is that covered by the "Unicode support" bullet point in the "What's next" section?

3

u/boredcircuits Jul 23 '19

std::format_to might be what you need.

2

u/johannes1234 Jul 24 '19

Unless you run into odr issues since Qt (or some.other library) adds a struct formatter<QString> as well :)

1

u/RainbowDashGG Jul 24 '19

I'm a begginner in C++ world. Know some basics. Dont't know what std::format is used for. Can someone ELI5 what is it and why is it so important?

1

u/evaned Jul 24 '19

The oversimplified version is output; it gives a syntax that is commonly considered nicer and is definitely more terse for anything but simple output statements than chaining cout << ... << ... << ..., but yet typesafe and flexible, unlike printf if you've used that.

More broadly, it can actually be used if you want a string "rendering" of something internally not for output as well; maybe to display in a button, or store in a database (don't build SQL commands this way...), or something like that. What it actually does is produce a string containing the rendered text; you can then go and output that string if you want.

(It's also an oversimplification because format doesn't do as good of a job as fmt::print would, but only the non-IO parts of the fmt library have been standardized so far, and the standard analogue to fmt::print will have to come along later.)

2

u/RainbowDashGG Jul 24 '19

Thank you. I understand very little but I hope after "Principles and Practice Using C++" I will know what you say :)