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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With