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
6
votes
2 answers

Is it possible to format number with thousands separator using fmt?

Is it possible to format number with thousands separator using fmt? e.g. something like this: int count = 10000; fmt::print("{:10}\n", count); update I am researching fmt, so I am looking for solution that works with fmt library only, without…
Nick
  • 9,962
  • 4
  • 42
  • 80
5
votes
2 answers

std::format %z specifier not working for local_time

I want to get a timestamp in the format "YYYY-MM-DD hh::mm::ss.fff +zz". I thought I could do this with the %z specifier for std::format but I'm getting this error on MSVC: error C7595: 'std::basic_format_string
MHebes
  • 2,290
  • 1
  • 16
  • 29
5
votes
1 answer

Using std::format with types that have operator<<

With the fmt library, you can easily format types that have operator<< defined. As explained here, you extend ostream_formatter with one line of code: template <> struct fmt::formatter : ostream_formatter {}; Now you can do…
Rob N
  • 15,024
  • 17
  • 92
  • 165
5
votes
3 answers

C++ 23 Does std::print or std::println flush the output stream?

Prior to C++ 23, when using std::cout to send output to stdout, there is a distinction between the following two ways of adding a new line at the end of the printed string: std::cout << "Hello World\n"; std::cout << "Hello World" << std::endl; The…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
5
votes
3 answers

eigen3 with libfmt >= 9.0

I used to be able to pass Eigen3 arrays/matrices to spdlog, which internally uses libfmt. Starting with libfmt 9.0.0, these types are no longer formatted by libfmt without further code. Custom types are supported in fmt by specializing…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
5
votes
3 answers

How do I print a vector of chars using fmt?

I have a const std::vector - not null-terminated. I want to print it using the fmt library, without making a copy of the vector. I would have hoped that specifying the precision would suffice, but the fmt documentation says that : Note that a…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
5
votes
1 answer

How to figure out the length of the result of `fmt::format` without running it?

While answering this question about printing a 2D array of strings into a table, I realized: I haven't found a better way to determine the length of the result of a fmt::format call that to actually format into a string and check the length of that…
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
5
votes
1 answer

fmtlib: an API to return parts of the formatted number in result string?

Is there a fmtlib API to have something similar to std::to_chars, but also returning a set of string_view for every part of the resulting string - specifically, sign; integer part; decimal part; exponent sign; exponent itself? That could help create…
Mike Kaganski
  • 701
  • 6
  • 18
5
votes
1 answer

Why is fmt library not found after installing fmt and gcc compiler with brew?

I am using MACOS Mojave version 10.14.3 and need to use GNU compiler and not clang. I installed gcc compiler using brew install gcc. Then I installed fmt library using brew install fmt. I put #include at the top of my C++ script Then…
ananvodo
  • 371
  • 5
  • 13
5
votes
1 answer

What is the math behind * 1233 >> 12 in this code counting decimal digits

I am a bit confused how this short function from the C++ {fmt} library works. inline std::uint32_t digits10_clz(std::uint32_t n) { std::uint32_t t = (32 - __builtin_clz(n | 1)) * 1233 >> 12; return t - (n < powers_of_10_u32[t]) + 1; } I…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
5
votes
2 answers

Convert initializer_list to variadic templates

I use a formatting library called fmt (http://fmtlib.net/latest/). One of the possible use is : fmt::format("Hello, {name}! The answer is {number}. Goodbye, {name}.", fmt::arg("name", "World"), fmt::arg("number", 42)); I'd like to wrap this call in…
Oodini
  • 829
  • 1
  • 6
  • 16
4
votes
1 answer

Using {fmt} to format containers

I am trying to use {fmt} to print a std::vector or std::array like this: std::vector vec = {1., 2., 3.}; fmt::print("{:0.3f}\n", fmt::join(vec, ",")); Thing is, I hope to print a transformed vector: std::vector vec = {1., 2.,…
auzn
  • 613
  • 3
  • 14
4
votes
2 answers

Difference between C++ stream and print() - fillers

It has been a while since I posted any questions on StackOverflow; I hope I remember more or less the appropriate way to address a question (sorry in advance). I was playing with C++ streams and the FMT library, which provides a preview to C++23…
Vivian Miranda
  • 2,467
  • 1
  • 17
  • 27
4
votes
1 answer

Displaying steady_clock::now() using fmt library

I'm able to display the current time using C++ fmt library using std::chrono::system_clock and std::chrono::high_resolution_clock but unable to display time using std::chrono::steady_clock. Here is the minimum reproducible example: #include…
Aamir
  • 1,974
  • 1
  • 14
  • 18
4
votes
1 answer

Does fmt library handle printing std::chrono::time_point?

The documentation for the C++ fmt libary says, about the "chrono format specifiers": Specifiers that have a calendaric component such as 'd' (the day of month) are valid only for std::tm and not durations or time points. But, I can create…
Rob N
  • 15,024
  • 17
  • 92
  • 165
1 2
3
17 18