Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use std::format in c++20

Tags:

c++

clang++

fmt

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.

like image 903
Saereon Avatar asked Sep 03 '25 10:09

Saereon


2 Answers

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.

like image 103
Alan Birtles Avatar answered Sep 05 '25 00:09

Alan Birtles


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.
like image 39
eerorika Avatar answered Sep 05 '25 01:09

eerorika