19

I was trying to use the C++ format utility (std::format). I tried to compile this simple program:

#include <format>

int main()
{
   std::cout << std::format("{}, {}", "Hello world", 123) << std::endl;

   return 0;
}

When I try compiling with g++ -std=c++2a format_test.cpp, it gives me this:

format_test.cpp:1:10: fatal error: format: No such file or directory
    1 | #include <format>
      |

I have got GCC 10.2.0

cael ras
  • 361
  • 1
  • 2
  • 12

2 Answers2

15

According to this: https://en.cppreference.com/w/cpp/compiler_support there are currently no compilers that support "Text formatting" (P0645R10, std::format). (As of December 2020)

The feature test macro defined by that paper is __cpp_lib_format (also listed here), so you can write your code like this to check:

#if __has_include(<format>)
#include <format>
#endif

#ifdef __cpp_lib_format
// Code with std::format
#else
// Code without std::format, or just #error if you only
// want to support compilers and standard libraries with std::format
#endif

The proposal also links to https://github.com/fmtlib/fmt as a full implementation, with fmt::format instead of std::format. Though you have to jump through some hoops to link the dependency or add it to your build system and to deal with the license / acknowledgement if necessary.

Your example with {fmt}: https://godbolt.org/z/Ycd7K5

Artyer
  • 31,034
  • 3
  • 47
  • 75
  • 7
    It's 2022 and this still hasn't been implemented? – FreelanceConsultant May 14 '22 at 14:24
  • 3
    @FreelanceConsultant: If it hasn't been implemented, then why is it mentioned in books and references? I was reading about this library on https://en.cppreference.com/w/cpp/utility/format/format. Even if I'm running the example code snippet, I got the same error as above. I'm quiet confused why a feature that hasn't been implemented yet is mentioned a lot in documents. – Avinash May 29 '22 at 20:18
  • 1
    @Avinash Probably because the literature is ahead of actual development. The standards committee publish their standards documents first, then the literature is written and compiler development on those new features starts. This would be my guess as to what has happened – FreelanceConsultant May 30 '22 at 13:36
9

Text formating P0645R10 is implemented in

  • GCC libstdc++ 13
  • Clang libc++ 14 (but marked as incomplete)
  • MSVC STL 19.29 (VS 2019 16.10)

Source

franckspike
  • 2,039
  • 25
  • 18