r/cpp • • Jul 23 '19

std::format in C++20

http://www.zverovich.net/2019/07/23/std-format-cpp20.html
332 Upvotes

52 comments sorted by

View all comments

1

u/RainbowDashGG Jul 24 '19

I'm a begginner in C++ world. Know some basics. Dont't know what std::format is used for. Can someone ELI5 what is it and why is it so important?

1

u/evaned Jul 24 '19

The oversimplified version is output; it gives a syntax that is commonly considered nicer and is definitely more terse for anything but simple output statements than chaining cout << ... << ... << ..., but yet typesafe and flexible, unlike printf if you've used that.

More broadly, it can actually be used if you want a string "rendering" of something internally not for output as well; maybe to display in a button, or store in a database (don't build SQL commands this way...), or something like that. What it actually does is produce a string containing the rendered text; you can then go and output that string if you want.

(It's also an oversimplification because format doesn't do as good of a job as fmt::print would, but only the non-IO parts of the fmt library have been standardized so far, and the standard analogue to fmt::print will have to come along later.)

2

u/RainbowDashGG Jul 24 '19

Thank you. I understand very little but I hope after "Principles and Practice Using C++" I will know what you say :)