Can't use std::format in c++20
Asked Answered
T

2

31

I've been trying to use the std::format function included in C++20. As far as I can tell, clang 14 is supposed to support this feature, but for some reason I am receiving the following error: no member named 'format' in namespace 'std'. According to cppreference's compiler support chart, text formatting should be supported by clang, but I'm still receiving this error. I'm at a loss for what the issue is.

Tushy answered 7/4, 2022 at 6:53 Comment(12)
Did you tell the compiler via the command line that you wanted to use the C++20 standard?Durtschi
The resp. command line arg. is -std=c++20. (I'm not sure which standard the compiler uses by default i.e. if no -std arg. is given.)Presumption
Also note that you might have to specify the Clang standard library with -stdlib=libc++.Bellinzona
Clang can use either libstdc++ or libc++ as standard library implementation. The linked page says that std::format is implemented (with the limitations mentioned when you hover over the version number) in libc++ but not in libstdc++. Which one are you using with Clang? Assuming both are installed you can switch between them with -stdlib=libc++/-stdlib=libstdc++.Smolder
Clang doesn't support std::format libc++ does, are you using it or libstdc++ or some other standard library? Are you using real clang or Apple clang? Please show a minimal reproducible exampleConcussion
Looks like cppreference is wrong libcxx.llvm.org/Status/Format.htmlConcussion
@AlanBirtles the clang website says the same, they should have std::format in clang-14 libc++, so looks like it is godbolt uses a wrong libc++?Apprehension
@Apprehension certainly seems to be 14.0: godbolt.org/z/T1d5TodvnConcussion
Looks like its incomplete and disabled by default: github.com/llvm/llvm-project/blob/… godbolt.org/z/6r4bTh8hjConcussion
-fexperimental-library might also do the trick in some casesBassinet
What is the situation in 2024?Match
@Match Clang 17 gives compiler errors too.Certes
R
8

According to this, text formatting should be supported by clang

If you look closely, there is an asterisk in that cell:

14*

Below, it says:

* - hover over the version number to see notes

And when you hover, it says:

The paper is implemented but still marked as an incomplete feature. Not yet implemented LWG-issues will cause API and ABI breakage.

What's unsaid is that incomplete features are not enabled by default. But that makes sense since they wouldn't want users to depend on an API/ABI that will break. In my opinion, as also evidenced by this question, using green for this cell is misleading.

In conclusion, it's best to use the third party formatting library until the standard implementation of text formatting is complete, stable and non-experimental in major language implementations.


Other caveats:

  • You must include the header that defines std::format.
  • Clang doesn't use C++20 by default, so you must specify it explicitly.
  • Clang uses libstdc++ standard library on Linux by default (for compatibility with shared libraries), so in such case you won't be using the Clang's standard library by default, and libstdc++ hasn't implemented text formatting yet.
Regional answered 7/4, 2022 at 7:29 Comment(0)
C
25

std::format is not complete in libc++ 14 so is disabled by default. You need to pass the LIBCXX_ENABLE_INCOMPLETE_FEATURES parameter when building llvm to enable the feature.

std::format is now fully available in libc++17.

If you can't use the latest verson of libc++ can use https://github.com/fmtlib/fmt.

Concussion answered 7/4, 2022 at 7:29 Comment(0)
R
8

According to this, text formatting should be supported by clang

If you look closely, there is an asterisk in that cell:

14*

Below, it says:

* - hover over the version number to see notes

And when you hover, it says:

The paper is implemented but still marked as an incomplete feature. Not yet implemented LWG-issues will cause API and ABI breakage.

What's unsaid is that incomplete features are not enabled by default. But that makes sense since they wouldn't want users to depend on an API/ABI that will break. In my opinion, as also evidenced by this question, using green for this cell is misleading.

In conclusion, it's best to use the third party formatting library until the standard implementation of text formatting is complete, stable and non-experimental in major language implementations.


Other caveats:

  • You must include the header that defines std::format.
  • Clang doesn't use C++20 by default, so you must specify it explicitly.
  • Clang uses libstdc++ standard library on Linux by default (for compatibility with shared libraries), so in such case you won't be using the Clang's standard library by default, and libstdc++ hasn't implemented text formatting yet.
Regional answered 7/4, 2022 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.