65

If it doesn't, do you know what compiler or version will?

See cppreference/format.

vitaut
  • 49,672
  • 25
  • 199
  • 336
PorssiMies
  • 761
  • 1
  • 5
  • 5
  • Not another way of formatting output! When is this going to end? Still at least *pi* is now defined as a constant. (Personally I use the C style functions for output, and the C++ ones for input.) – Bathsheba Sep 03 '20 at 12:51
  • https://gcc.gnu.org/projects/cxx-status.html – bipll Sep 03 '20 at 12:51
  • 17
    @Bathsheba ?! C++ did not have *any* adequate way of formatting output before the adoption of fmtlib into C++20. fmtlib has the only usable API and is additionally hands down the fastest formatter. fmtlib isn’t “another way of formatting output”, it’s the *only* generally usable way of formatting output in C++. – Konrad Rudolph Sep 03 '20 at 12:54
  • In particular, there's a link to library status on that page with poorly formatted tables, and if you search for 'text formatting' on that page, there are three rows pertaining to it, with empty column 'Status' each. – bipll Sep 03 '20 at 12:56
  • @KonradRudolph: Yes of course you have a point, but I would have much rather had a decimal floating point type before all this stuff. The committee could at least have humoured me by reserving `decimal`. Of course it would be `_Decimal` in C. – Bathsheba Sep 03 '20 at 12:58
  • @bipll I already read that document but was not able to find anything. No "text formatting" either. – PorssiMies Sep 03 '20 at 13:07
  • @PorssiMies the link to lib status on that page, https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html – bipll Sep 03 '20 at 13:09
  • @PorssiMies: Alas that wasn't me, but Konrad. My contribution (if you can call it that) was little more than a rant about priorities. – Bathsheba Sep 03 '20 at 13:09
  • @PorssiMies Not implemented yet indeed but you can find some ersatz implementation which might be good enough, [for example](https://github.com/SerenityOS/serenity/blob/4842c8c90286b56e4bac6767c28076191ba5b00e/AK/Format.cpp#L455) – Luke Skywalker Jul 21 '21 at 15:50
  • 6
    I am seriously wondering what takes them so long. Essentially that library has been written long ago as a separate project. – shuhalo Sep 15 '21 at 19:06
  • @Konrad Rudolph std::printf cries in corner... – 0xB00B Dec 12 '21 at 09:11

5 Answers5

86

It's 2023 Now! Cheers!

GCC 13, CLANG 14 and MSVC 16.10/VS 2019 all have the {fmt} based std::format available in respective standard libraries.

Hopefully we'll have C++23 std::print available soon.


See Compiler support here :-

0xB00B
  • 1,598
  • 8
  • 26
  • 7
    Keep in mind though that {fmt} has more features than the C++20 . So, if you plan to use it then do a one-for-one switch of fmt::format to std::format, you'll need to carefully select which features you use. – Paul Deitel Jan 23 '22 at 14:19
  • 10
    I like how this is being kept up to date. – Alex Feb 09 '22 at 12:03
  • 1
    gcc trunk has recently implemented text formatting – Desmond Gold Nov 13 '22 at 22:39
  • funny. neither gcc nor clang can find the format include. i got the latest clang-17 and the latest gcc i can get on my system, which is version 12.1.0. am I doing something wrong? because everyone is "hooray, we got the new features", and clearly I do not. – FalcoGer Jul 18 '23 at 00:14
  • @FalcoGer with clang you need to also use llvm's libc++, `-stdlib=libc++` should do the trick. As for GCC, you need version 13 minimum. – 0xB00B Jul 18 '23 at 19:34
43

EDIT: Support for format is available since GCC 13. For someone who want to find compiler support for another libraries or features, please refer to the compiler support link below from cppreference.com

Does gcc support C++20 std::format?

Not yet!

There's no compiler support yet, not even gcc 11. See Text formatting in Library features.

Compiler support for C++20 library features

John Park
  • 1,644
  • 1
  • 12
  • 17
22

As of today (November 15, 2022), GCC 12.2 does not support this feature. Or rather, its standard library implementation doesn't support it (cause it's a library feature, not the compiler-one).

NOTE: The feature is mentioned as implemented in GCC 13.0 (link)

You can try Clang 14.0.0 and upward for now, but note following:

In libc++ of Clang 14.0.0 std::format support added but: "The paper is implemented but still marked as an incomplete feature. Not yet implemented LWG-issues will cause API and ABI breakage" (link)

In libc++ of Clang 15.0.0 and later, this feature can be enabled with the -fexperimental-library compiler flag (link)

Also you can try MSVC 16.10 and upward. Support of std::format is mentioned as complete.


Standard library current status can be seen here:

Ihor Baklykov
  • 543
  • 7
  • 24
  • I use clang17 and even with the `-fexperimental-library` flag it doesn't compile, complaining that it can't find #include or #include . Can you elaborate on how to make it work? – FalcoGer Jul 19 '23 at 14:37
  • @FalcoGer are you using `-stdlib=libc++` ? Here's an example: https://godbolt.org/z/K5ee4b9r4 – Ihor Baklykov Jul 20 '23 at 12:22
  • If I use that it complains that it can't find iostream. – FalcoGer Jul 22 '23 at 17:53
  • But `-fexperimental-library` works only with `-stdlib=libc++` because AFAIK Clang by default uses **GCC's stdlibc++** and you have to manualy instruct it to use **Clang's libc++**. *"If I use that it complains that it can't find iostream."* - check if libc++ is installed – Ihor Baklykov Jul 24 '23 at 08:57
  • turns out discord was screwing me over with it's dependency on libc++-14. installing libc++17-dev and pointing clang to the libc++experimental library with `-L` solved the problem. discord is now a flatpak application for me. – FalcoGer Jul 24 '23 at 15:25
6

GCC 13 has added support for std::format. According to cppreference, as of GCC 13, no gaps remain in its C++20 support (in both the core language and the standard library).

This bugzilla ticket notes that some C++23 extensions to std::format have not yet been implemented.

GCC 13 can be expected around April 2023.

The GCC trunk installation on compiler explorer includes std::format support.

OpenSUSE Tumbleweed (as of 2023-03-19) and the upcoming Fedora 38 include GCC 13.

PFee
  • 249
  • 2
  • 10
  • Is this not available in any of the latest clang versions? I see it only work on clang trunk. It works with gcc 13 and onwards now. – nullspace Sep 02 '23 at 21:58
5

Now libstdc++ has implemented it! https://gcc.gnu.org/pipermail/libstdc++/2022-November/054991.html

SodaCris
  • 358
  • 4
  • 7