> obviously you should not use sprintf, you should use C++ iostreams
Friends don't let friends use iostreams.
> obviously you should not use sprintf, you should use C++ iostreams
Friends don't let friends use iostreams.
Agreed. Had this discussion enough times that I put my thoughts in an article. <iostream> is just spectacularly bad.
https://www.moria.us/articles/iostream-is-hopelessly-broken/
printf("The object is ");
print_object(object, stdout);
printf("\n");
Does fmt support printing a custom type without breaking the format string and without using a temporary string?fmt has a “buffer” class which is used as an interface between formatters and their two primary use cases, which are formatting to strings and formatting to files. A buffer is an abstract base class which exposes a pointer to a region of memory and has a virtual function to flush the output (sort of). When you call fmt::format or fmt::print, you’re getting std::back_insert_iterator for a buffer.
I would describe this as “surgical usage of a virtual function” because it is used exactly in a place where it is not called often (you mostly write to a buffer, and flush it less often) and its use reduces the number of template instantiations in your project. That said, the ergonomics for custom formatters is not great.
https://fmt.dev/latest/api.html#formatting-user-defined-type...
It may look like a lot to implement, but it worked flawlessly for me several years ago for a simple case, just by copying and pasting the example code there.
I live in a country where the elders of the language decided that we don't format floating point numbers with a decimal point but use a decimal komma instead.
So PI is not 3.14 but 3,14 instead.
Just imagine what pain you have to go through if you want to parse a .csv file written by an application that "tried to do everything right and use locale".
Microsoft Excel will helpfully use semicolons as field separators in this case, making parsing CSVs that came out of it in an unknown locale situation even more fun!
Well, there are some exceptions, like actually caring about the original datetime, but they are rare.
How would you write 3,141.592?
if it's money, you use the . as the decimal separator everywhere. if it's just a number, in the French parts, it's , in the German parts the .
The thousands separator is ' everywhere