In a related question ("std::string formatting like sprintf") I learned about this awesome new C++20 header <format>.
However, there seems to be no supporting compiler. Is this correct or is there a way to use it anyway?
I'm using g++ 9.3 with the -std=c++2a
flag and the library <format>
is not recognised.
#include <format> // fatal error: format: No such file or directory
#include <iostream>
int main(){
std::cout << std::format("Hello {}!", "World");
}
g++-9 test.cpp -o test -std=c++2a
Use libfmt
. The <format>
header is essentially a standardized libfmt (with a few small features removed, if I remember correctly).
There is a libfmt-dev
package available on debian/ubuntu.
I currently use this header as a replacement for the format
header:
#include <fmt/core.h>
#include <fmt/ranges.h>
namespace std
{
template<typename... Args>
inline auto format(Args&&... args) -> decltype(fmt::v7::format(std::forward<Args>(args)...))
{
return fmt::v7::format(std::forward<Args>(args)...);
}
}
This is experimental: I don't know exactly what are the differences between std::format
and fmt::format
.
Life is full of surprises
As of the time of writing, MSVC and Clang are the only 2 compilers supporting the header.
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