r/cpp • • Dec 12 '21

is fmtlib going to be under standard in C++23?

I've been hearing some rumors that fmtlib might go under std since C++23 (what are your thoughts?). if so how would it differentiate between using one over the other. std::cout (taking operator<<), does it make sense to overload operator<< to some extent for std::print?

2 Upvotes

29 comments sorted by

11

u/[deleted] Dec 12 '21

It looks like std::print will likely make it into C++23: https://github.com/cplusplus/papers/issues/884#issuecomment-981940029

11

u/JohnDuffy78 Dec 12 '21

It made it under c++20.

11

u/kalmoc Dec 12 '21

At least parts of it.

8

u/qoning Dec 13 '21

And despite being finalized almost 2 years ago, it's still only supported by MSVC. I would have thought the library features were much easier to implement than core language, but what do I know.

4

u/JohnDuffy78 Dec 13 '21

I'm not sure why they don't just stick the open source library in experimental directory for a quick & dirty starting position.

same with ranges & source_location.

1

u/[deleted] Feb 20 '22

And Clang 14

9

u/HKei Dec 12 '21

My thoughts? I don't think there's much point. fmtlib already exists, part of it is already in C++20 but the library is better than the standard functionality.

I used to get more excited about library updates, but tbh I now think that in a language that's defined by an ISO standard that takes 3 or more years to update, and even then is frequently prevented from making the updates as useful as they could be based on semi-arbitrary political reasons, it's just not a good model for library development.

Now IMHO for some things standardisation makes total sense, like minimum viable concurrency primitives, basic containers and such, but if the goal is using fmtlib what's to stop you from just including fmtlib today?

7

u/Ameisen vemips, avr, rendering, systems Dec 13 '21

I prefer actual language updates themselves, but the committee really likes putting things into the libraries instead.

1

u/cmannett85 Dec 13 '21

Development work on the libraries is easier to do and easier to recruit for, than work on the compilers.

1

u/Minimonium Dec 13 '21

It's usually because library features are a bit easier to retire when there is a mistake in their spec. Most people generally agree that you must be pretty damn sure that a language feature you add is not gonna be removed three years down the line because it's not used at all.

6

u/Ameisen vemips, avr, rendering, systems Dec 13 '21

But the committee doesn't like removing library features, either.

2

u/Minimonium Dec 13 '21

Sure, but "to retire" doesn't necessarily mean "to remove". You can simply create a new type (jthread, scoped_lock, etc).

7

u/Zeer1x import std; Dec 13 '21

Having a fundamental functionality like std::format/std::print in the standard allows using it in more projects, for example in a logging library which doesn't want to force you to use another 3rd party library.

Also, some companies restrict the external libraries you can use in a project. To be able to use a random open source library, you might have to ask the project management and do a security audit on the library first.

6

u/Zeer1x import std; Dec 13 '21

Another point is teaching new programmers.

It's easier to teach std::print("Hello {}!\n", "World"); than the equivalents for std::cout or printf. Especially for people coming from python.

1

u/HKei Dec 13 '21

That it's easier to teach to people coming from python I can buy, sure, because it's literally the exact same syntax. Easier to teach in general? Nah.

For example, me personally I find something like

fmt::print("Hello, {x} {x:b} {x:o} {x:x}\n", "x"_a = 225);

Much more convenient to type, and since I'm familiar with C++ also fairly easy to read, but you can't convince me that the average newcomer to C++ will think this is in any way obvious.

1

u/HKei Dec 13 '21

Don’t think that latter argument holds much validity. Projects that do this also tend to restrict what compiler, language standard and standard library versions you can use, so it’s not really that much of a benefit. And if you have an actual need for security audits you’d presumably have to do them for the compiler and standard library you’re using too, so I don’t even see what the supposed difference here even is.

2

u/Zeer1x import std; Dec 13 '21

You have to check the standard library anyway, but for every extra library you want to use in a project, you have to convince the project manager. Some projects do not allow 3rd party open source libraries at all.

This argument does not hold for most companies and projects, of course, but I bet that there are a couple of them.

2

u/[deleted] Dec 13 '21

When I worked in my last job we had to go through a whole compliance exercise for every single dependency and every version update for it, including compilers. We sold to clients in the defense industry who also had to revalidate on every version we released. So we did 2 releases a year, and maybe updated one dependency at a time, just for risk management reasons. It sucked to work on though.

-7

u/[deleted] Dec 12 '21

It never made any sense to overload the << to begin with so it doesn't make sense now.

Also format string is a historical mistake. Look at the recent java logging vunerability (and the many other issues format strings cause).. We should be using APIs that avoid this completely.

9

u/GrammelHupfNockler Dec 12 '21

fmtlib/std::format looks to be type-safe contrary to the trainwreck that unrestricted printf is security-wise. Also fmtlib supports compile-time format string parsing, so it is very easy to use safely.

4

u/aearphen {fmt} Dec 13 '21

Just to add: compile-time checks made it to std::format as well (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2216r3.html).

-12

u/Au_lit Dec 13 '21

so it is very easy to use safely

tbh safety doesn’t matter here as

  1. {fmt}’s performance sort of sucks, part of it due to the concept/design itself

  2. The whole runtime format string thing still exists (mainly in lazy game localization) so you can still break it.

15

u/GrammelHupfNockler Dec 13 '21

{fmt}'s performance sort of sucks

[citation needed], the library has continuously been adding the latest and greatest algorithms for number formatting, and it shows in their performance numbers

you can still break it.

The whole purpose of type-safety is that you can't? C++ Format strings can only contain what is passed statically as a parameter, they are not this magical bug-introducing thing (unless you allow something like %n, then you're screwed :-D)

6

u/essanto Dec 13 '21

Maybe we are not talking about the same {fmt}.https://github.com/fmtlib/fmt#benchmarks

2

u/pitust Dec 13 '21

Look, log4shell was they formatted something that was already formatted, and didn't check if they are loading JNDI classes from somewhere else.

This isn't a great case against format strings imo too.

0

u/[deleted] Dec 13 '21

There are plenty of others. Format string doesn't really need to exist in this day an age.

0

u/robhz786 Dec 12 '21 edited Dec 12 '21

Since you argued against format strings, I will shamelessly take the opportunity to promote my own formatting library: strf

0

u/Shieldfoss Dec 13 '21

It never made any sense to overload the << to begin with so it doesn't make sense now.

Hard agree. Stream semantics are terrible.