Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 834
Mert Köklü Avatar asked Nov 25 '19 02:11

Mert Köklü


People also ask

Does clang support C++20 format?

Clang doesn't use C++20 by default, so you must specify it explicitly. Clang uses libstdc++ standard library on Linux by default (for compatibility with shared libraries), so in such case you won't be using the Clang's standard library by default, and libstdc++ hasn't implemented text formatting yet.

What is C++ format?

C++ printf is a formatting function that is used to print a string to stdout. The basic idea to call printf in C++ is to provide a string of characters that need to be printed as it is in the program. The printf in C++ also contains a format specifier that is replaced by the actual value during execution.


2 Answers

What are the advantages over printf

Type safety. For printf, the programmer must carefully match the format specifier to the type of the argument. If they make a mistake, the behaviour of the program is undefined. This is a very common source of bugs, especially for beginners.

To be fair, decent compilers diagnose these mistakes as long as a constant format string is used, as long as the programmer has remembered / knows how to enable the warnings. Regardless, it is much more convenient, and safer to use template argument deduction to choose the formatted type automatically.

Furthermore, there is no way to extend printf to support printing class types.

or std::cout

Stream manipulators are quite cumbersome and verbose, and have inconsistent behaviour. Some manipulators are "sticky", affecting all subsequent insertions, while others only affect only a single insertion.

The lack of separation between the format and the arguments in the iostream API arguably makes it harder to comprehend the (intended) result.

How can I use it

Either wait for your compiler / standard library implementation to support it. Or if don't want to wait, use the original non-standard version instead. Then follow the documentation.

The normative specification is the C++ standard. There are also websites that present the standard, including this library in a more convenient format. Another good source of information is the standard proposal. The repo for the non-standard version (linked in previous paragraph) also has tons of documentation, although there will be differences to what will be in the standard.

and someone give an example of it?

Here you go (adapted from the documentation of libfmt):

std::string s = std::format("I'd rather be {1} than {0}.", "right", "happy");
like image 172
eerorika Avatar answered Oct 16 '22 17:10

eerorika


C++20's std::format is primarily just the inclusion of the fmt library that many people are already using (we use it as part of the spdlog logging framework).

So, if you want to use it, you can just download fmt.

As for advantages, it has the type-safety of streams but without the verbosity (legacy-C printf is concise but neither type-safe nor extensible). Here's an example (slightly modified) from our own code base:

std::string idStr = fmt::format("prefix.{:05d}.suffix", id);

which would have otherwise required the rather less than concise standard C++:

std::string idStr;
{
    std::stringstream ss;
    ss << "prefix." << std::setfill('0') << std::setw(5) << id << ".suffix";
    idStr = ss.str();
}
like image 15
paxdiablo Avatar answered Oct 16 '22 15:10

paxdiablo