3 ms·
It would have been possible to, as a special case, type check the formatting, C compilers began doing that in the 1990s IIRC.
by tialaramex 3y ago
It would have been possible to, as a special case, type check the formatting, C compilers began doing that in the 1990s IIRC.
- throwway120385 3y agoAnd there are ways of signaling to a compiler that your function accepts a format string and a series of typed arguments in case you're wrapping a C-style printf function.
- layer8 3y agoIt wouldn’t have added support for custom types to printf(), which was an important motivation.
- tialaramex 3y agoThat's true, however neither the mechanism to do this in I/O streams, nor the weighty mechanism for std::format come close to what Rust's derive macro offers for Debug, and in my experience asking for so much means you often won't get anything because programmers are lazy. That is, for a custom type in Rust having rudimentary Debug output is one word added to the list of derive macros, the word "Debug". Almost everybody will write that, it's barely effort. To get that in C++ I/O Streams you need to write an operator overload function and must be careful to write it correctly. Trivial types usually don't bother, but many C++ programmers are comfortable writing this for an important type. For std::format you need to write a template which is already pretty scary, and then it needs to implement custom parse and format steps, this is because std::format allows your custom type to define its own custom formatting rules. This is extremely powerful, but everybody must pay for this power. I have not seen most people bother doing all this at all.
- layer8 3y agoYou can write operator<< as a free (non-member) function yourself for classes that don’t implement it. Furthermore, it’s not necessarily the same output that you want for debugging.