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

What's the simple way to print or format a custom type with libfmt?

Suppose I've defined some type, e.g. struct Foo { int a; float b; }; If I want to stream it to an ostream, I write an operator<< function, e.g.: std::ostream& operator<<(std::ostream& os, const Foo& foo) { return os << '(' << a << ',' << b <<…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
4
votes
1 answer

Custom formatting of the elements of std::vectors using the fmt library

While I can use to readily output the contents of a std::vector, I'm at a loss to format the display of its elements according to my preferences. #include #include int main() { double x1 =…
marital_weeping
  • 618
  • 5
  • 18
4
votes
1 answer

Generating compile time functions string for formatting strings with libfmt

I want to create a nice table in stdout. The table has a lot of headers that are mainly compiletime strings. For example: std::cout << fmt::format("|{0:-^80}|\n", "File Information"); The above prints: |-----------------------------File…
DEKKER
  • 877
  • 6
  • 19
4
votes
1 answer

fmt Library - Formatting to a (compile-time) string_view

I would like to use the fmt library to create a string_view from my format args. There is plenty documented about passing in a compile-time string as the format string, however, I want to output a compile-time string, so that I may use it in other…
stellarpower
  • 332
  • 3
  • 13
4
votes
1 answer

Technical background to the C++ fmt::print vs. fmt::format_to naming?

Why is it fmt::format_to(OutputIt, ...) and not fmt::print(OutputIt, ...)?? I'm currently familiarizing myself with {fmt}, a / the modern C++ formatting library. While browsing the API, I found the naming a bit disjoint, but given my little-to-no…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
4
votes
1 answer

C++ {fmt} library, user-defined types with nested replacement fields?

I am trying to add {fmt} into my project, and all is going well, except I hit a little snag when trying to add a user-defined type for my simple Vec2 class. struct Vec2 { float x; float y; }; What I would like is to be able to use the same format…
BeigeAlert
  • 197
  • 1
  • 11
4
votes
1 answer

Overloading between printf functions and fmt functions

I have a class that takes a C printf set of variadic arguments in its constructor, like this: class Foo { public: Foo(const char* str, ...) __attribute__((format(printf, 2, 3))); Now I want to be able to use the fmt library with this class. If…
MadScientist
  • 92,819
  • 9
  • 109
  • 136
4
votes
1 answer

How do I efficiently forward the formatting arguments when implementing a formatter for a custom type

I am trying to write a custom formatter to help me print vectors. I am attempting to maintain the format specifiers so that each item in the vector is formatted the same way. I have taken most of my inspiration from this tutorial, and the…
Troyseph
  • 4,960
  • 3
  • 38
  • 61
4
votes
2 answers

Quote a string using {fmt}

Is there any way to print a string as quoted using {fmt}? Here is an example code showing what I want to achieve: fmt::print("Hello {}!", "Terens"); I want the code to print Hello "Terens"! instead of just Hello Terens!. EDIT: I want to use the API…
Terens Tare
  • 129
  • 1
  • 14
4
votes
2 answers

Cannot format argument from within spdlog

I am trying to use spdlog. I incorporated it with my code, but now I am getting the following error: ....fmt\core.h(1016): error C2338: Cannot format argument. To make type T formattable provide a formatter specialization:…
CygnusX1
  • 20,968
  • 5
  • 65
  • 109
4
votes
1 answer

Linking errors with fmt: undefined reference to `std::string fmt::v6::internal::grouping_impl(fmt::v6::internal::locale_ref)'

In our project, we decided to use the latest fmt version(6.2.0) in our project and use mainly the printf functionality as we had our logging where we are using printf extensively. I built the libfmt.a on our Linux box using the CMakeLists.txt…
psingh
  • 47
  • 1
  • 4
4
votes
1 answer

Convention on printing STL containers for approval tests

I'm writing approval tests using the excellent ApprovalTests.cpp library. This library automates generation of "snapshots" of results from a function. Snapshots are generated serializing results of type T to a file using the ostream& operator<< (T…
Alessandro Pezzato
  • 8,603
  • 5
  • 45
  • 63
4
votes
1 answer

How to write a formatter for fmt using wchar_t?

I want to specialize a formatter template so I can write: rect rc(0, 0, 100, 20); wstring buf = fmt_msg(L"{0}", rc); I have this: ea::wstring vfmt_msg(const ea::wstring &msg, fmt::wformat_args args) { std::wstring s_ =…
Mark Kosyk
  • 115
  • 1
  • 8
4
votes
1 answer

How to get fmt::format to work with wchar_t?

I want to have fmt::format return a wstring from EA's STL, but when I try to modify this code from the docs—which works fine: // Prints formatted error message. void vreport_error(const char *format, fmt::format_args args) { fmt::print("Error:…
Mark Kosyk
  • 115
  • 1
  • 8
4
votes
1 answer

Issue on using {fmt} as a dependency with CMake

I'm trying to apply modern CMake practices in my project. And I've came up with an issue with the {fmt} library dependency. The structure of a project is the following (in a brief): dev/ | +--- fmt/ *unpacked archive of 4.1.0 version* | +---…
kola
  • 310
  • 2
  • 10