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
65
votes
5 answers

Does GCC support C++20 std::format?

If it doesn't, do you know what compiler or version will? See cppreference/format.
PorssiMies
  • 761
  • 1
  • 5
  • 5
28
votes
2 answers

How can I use C++20 std::format?

C++20 introduces std::format. What are the advantages over printf or std::cout? How can I use it and someone give an example of it?
Mert Köklü
  • 2,183
  • 2
  • 16
  • 20
26
votes
6 answers

C++ string formatting like Python "{}".format

I am looking for a quick and neat way to print in a nice table format with cells being aligned properly. Is there a convenient way in c++ to create strings of substrings with certain length like python format "{:10}".format("some_string")
chrise
  • 4,039
  • 3
  • 39
  • 74
24
votes
2 answers

std::format of user-defined types?

In C++20 - how do you make a user-defined type compatible with std::format? For example, let's say I have a type called Point: struct Point { int x; int y; }; with its operator<< defined: inline std::ostream& operator<<(std::ostream& o,…
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
22
votes
2 answers

Can't use std::format in c++20

I've been trying to use the std::format function included in C++20. As far as I can tell, clang 14 is supposed to support this feature, but for some reason I am receiving the following error: no member named 'format' in namespace 'std'. According to…
Saereon
  • 229
  • 1
  • 2
  • 8
20
votes
2 answers

How to create a function that forwards its arguments to fmt::format keeping the type-safeness?

I have two broadly related questions. I want to make a function that forwards the arguments to fmt::format (and later to std::format, when the support increases). Something like this: #include #include constexpr auto…
Daniel
  • 2,657
  • 1
  • 17
  • 22
19
votes
2 answers

Format no such file or directory

I was trying to use the C++ format utility (std::format). I tried to compile this simple program: #include int main() { std::cout << std::format("{}, {}", "Hello world", 123) << std::endl; return 0; } When I try compiling with g++…
cael ras
  • 361
  • 1
  • 2
  • 12
15
votes
1 answer

What are the differences between libfmt and std::format?

I am aware that the c++20 format proposal is a formalization of parts of libfmt, and that libfmt is a compliant implementation of that formalization. However, it's my understanding that libfmt provides additional functionality beyond that specified…
Walrus
  • 405
  • 4
  • 10
14
votes
1 answer

Convert a vector to string with fmt library

How to remove {} from the output? #include #include #include #include int main () { std::vector v = {1,2,3}; std::string s = fmt::format("{}", v); std::cout << s << '\n'; // output :…
sadig
  • 431
  • 1
  • 4
  • 9
13
votes
1 answer

How to format floating point numbers with decimal comma using the fmt library?

I want to use the fmt library to format floating point numbers. I try to format a floating point number with decimal separator ',' and tried this without success: #include #include #include struct numpunct :…
schoetbi
  • 12,009
  • 10
  • 54
  • 72
12
votes
2 answers

FMT C++ library: allow user to set format specifiers for custom type

I have a custom type, for example struct custom_type { double value; }; I want to set a custom FMT formatter for this type. I do the following and it works: namespace fmt { template <> struct formatter { template…
Lecko
  • 1,275
  • 1
  • 17
  • 32
11
votes
3 answers

How can I avoid using #define macros in C++ in a case where I have to concatenate two const char* variables?

I would like to remove the reliance of #define macros in my code, and I'm unable to achieve the same functionality with constexpr. To be practical, consider the following example: #define PRODUCT_NAME "CloysterHPC" constexpr const char* productName…
10
votes
1 answer

How to format pointers using fmt library?

I'm taking up some folks advice and looking into the fmt library: http://fmtlib.net It appears to have the features I need, and claims to support %p (pointer), but when compiling my code which uses a %p I get a long string of template errors…
Mordachai
  • 9,412
  • 6
  • 60
  • 112
9
votes
5 answers

C++ namespace "std" has no member "format" despite #include

I am new to C++. I am trying to store the current date and time as a string variable. At this question, I found an answer, and installed the date.h library. However, when I try to use the code provided, I am met with the error: namespace "std" has…
Enderbyte09
  • 409
  • 1
  • 5
  • 11
9
votes
1 answer

How to use fmt library in the header-only mode?

Having a hard time using the header-only mode of fmt library. Here is what I tried in details: I downloaded fmt7.1.3 from https://fmt.dev/latest/index.html, only put the directory fmt-7.1.3/include/fmt in a directory ([trgdir]) and wrote a test.cpp…
Ethanabc
  • 311
  • 2
  • 7
1
2 3
17 18