Questions tagged [fmt]

The {fmt} formatting library and the C++ text formatting and output facility (C++20 `std::format` and C++23 `std::print`).

{fmt} is an open source formatting library for . It is similar in functionality to and sprintf. The formatting facility defined in the <format> header is based on it and provides a subset of {fmt}'s functionality.

259 questions
4
votes
4 answers

How to pass not variadic values to fmt::format?

I'm playing with the great fmt C++ library to format strings more gracefully. And I'd like to pass a non-variable arguments list to fmt::format. It could be a std::vector, or std::string, or whatever, but it will always match the format string. So…
Kyone
  • 513
  • 5
  • 17
3
votes
1 answer

fmt::format library "malfunctioning"?

#include #include #include using namespace std; int main(int argc, char **argv) { cout<<"Expected: ' -10'\n got:'"<10d}",-10)<<"'\n"; cout<<"Expected: '-000000010'\n …
George Kourtis
  • 2,381
  • 3
  • 18
  • 28
3
votes
2 answers

fmt compile time format string check without generating asm code for the check?

Is there anyway to ask fmt library to do compile time check on the format string against the argument types without generating any asm code (to be compiled out)? For example, #include #include int main() { …
HCSF
  • 2,387
  • 1
  • 14
  • 40
3
votes
2 answers

fmt::format() with chrono local date is different than with iostream

To be portable, while I'm using the {fmt} library for formatting dates and times, I encounter a small problem with printing times. Probably fmt treats time as local time, whereas prints time as UTC? Minimal example: #include…
Kaaml
  • 31
  • 2
  • 3
3
votes
1 answer

C++ fmtlib: How to leverage builtin format specification parsing for outputting custom type?

This is related to using the fmtlib c++ library. I have a templated rational type that contains a numerator and denominator that I am formatting by specializing fmt::formatter as described in the documentation. I would like to accept the same format…
RandomBits
  • 4,194
  • 1
  • 17
  • 30
3
votes
1 answer

Issue with libfmt and locale: Alignment is not correct

I try to format some floating point numbers using the appropriate number separator character. In the following example, the last output line is not formatted correctly (It is shifted by one to the left). Program: #include #include…
beedaddy
  • 75
  • 4
3
votes
4 answers

Display unicode character in fmt lib C++

I want to display the infinity symbol ∞ which has Unicode U+221E. I am currently using the fmt library, it is supposed to have a lot of support and be cross-platform. fmt::print("", fmt::styled("∞ >", fmt::emphasis::bold |…
Alix Blaine
  • 585
  • 2
  • 16
3
votes
2 answers

How to print the current time with fractional seconds, using fmt library

This code #include #include #include ... auto now = std::chrono::system_clock::now(); fmt::print("The time is: {:%Y-%m-%d %H:%M:%S}\n", now); Prints this: The time is: 2023-01-02 15:51:23 How do I get it to…
Rob N
  • 15,024
  • 17
  • 92
  • 165
3
votes
1 answer

Is there a way to use fmt::output_file to write wstring to a file?

I am trying to write formatted wstring logs to file using fmt. I can do it by using fmt::format(...) and fmt::vprint but I want to use fmt::output_file to do it, however I am getting compile error. Below code shows two ways that are working and the…
StewieGGriffin
  • 349
  • 1
  • 4
  • 14
3
votes
1 answer

fmt formatter for private class type

How does one define a formatter for private class types that are inside of namespaces? Buidlable code at https://github.com/cmorganBE/fmttest. I have a number of classes like: namespace example_namespace { class someclass { private: enum…
Chris Morgan
  • 1,277
  • 1
  • 13
  • 33
3
votes
2 answers

std::format pointer error C3615: consteval function 'std::_Compile_time_parse_format_specs' cannot result in a constant expression

How to fix (stdC++20 mode VS2022) #include #include auto dump(int *p) { std::string resultstring = std::format(" p points to address {:p}", p); resulting in: error C3615: consteval function…
GeePokey
  • 159
  • 7
3
votes
2 answers

How to flush fmt output in debug mode?

I'm experimenting with fmt and I do get output from code below #include int main() { fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, "Hello, {}!\n", "world"); fmt::print(fg(fmt::color::floral_white) |…
KcFnMi
  • 5,516
  • 10
  • 62
  • 136
3
votes
1 answer

How to benchmark fmt::format API on https://quick-bench.com

I am trying to understand and benchmark fmt::format API ( https://fmt.dev/latest/api.html ). I wrote a simple test in compiler explorer i.e, https://godbolt.org/z/fMcf3nczE. #include #include static void…
3
votes
1 answer

C++ {fmt} library: Is there a way to format repeated format fields?

I have a program with many formatted write statements that I'm using the fmt library for. Some of them have many fields, say 100 for example purposes, something like this: fmt::print(file_stream, "{:15.5g}{:15.5g}{:15.5g}/*and so on*/", arg1, arg2,…
wand_swe
  • 43
  • 5
3
votes
1 answer

How to make `template<> struct fmt::formatter` friend of Foo

I have a class Foo with a private member x, and I want to be able to print instances of Foo via fmt::print. [Demo] #include class Foo { public: explicit Foo(int i) : x{i} {} private: int x{}; }; template<> struct…
rturrado
  • 7,699
  • 6
  • 42
  • 62