6 ms·
> C already has rock-solid string formatting options Don't troll.
by GFK_of_xmaspast 11y ago
> C already has rock-solid string formatting options
Don't troll.
- duaneb 11y agoDo you have an actual response? How is iostream any better than *printf? At least you can read the format of the former without needing to google un-googleable operators.
- msbarnett 11y ago> How is iostream any better than *printf? Type checking.
- duaneb 11y agoYou already have to do odd casting to get around compiler warnings when passing invalid types to a format—I don't buy this anymore.
- simoncion 11y ago> I don't buy this anymore. You should: https://news.ycombinator.com/item?id=10775022 https://news.ycombinator.com/item?id=10775022
- jmgao 11y agoprintf has type checking in every major compiler. You just need to annotate all of your printf-y functions with attributes to tell the compiler "this function does printf, the format string is the nth argument, and the format arguments start at the mth argument". It doesn't work with non-constexpr format strings, but there aren't many justifications for those.
- simoncion 11y ago> How is iostream any better than [<star>]printf? What consistent mechanism do you use to printf an instance of an arbitrary user-defined type? With ostream, you implement ostream& operator<<(ostream&, const Class&) and you're done. with printf, you have to have a convention and remember to stick to it. (Do I call .toString(), .stringize(), .getString()... ??) For one-off things, printf is great. You take it from me when you pry it from my goddamn cold, dead, hands. However, for real work io*stream are rather nice.
- ambrice 11y agoWith printf, you do have to look at the docs to see what the string conversion function is. With iostream, you still have to look at the docs to see if the ostream operator is implemented for the class, it's certainly not universal.
- simoncion 11y agoI never said io*stream support was universal, [0] I said that it provided a _consistent_ mechanism. :) [0] I mean, it's obvious that anything that you don't get for free out of the box is bound to _not_ be universal. Edit: Actually, the thing you get out of the box when you present an instance of a non-trivial user-defined class to both printf and ostream is equally useless... unless you're debugging and are interested in the address of the instance of an object.
- ambrice 11y agoRight, I'm just questioning whether this consistency buys you anything. Either way you're checking the docs to see how to print an object.
- simoncion 11y ago> ...I'm just questioning whether this consistency buys you anything. It buys you the same thing that API consistency always buys you: reduced cognitive overhead when dealing with a given API. :) Edit: As GFK_of_xmaspast mentions [0] instances of non-trivial classes are not compatible with the default implementations of "operator <<" in ostream. So, you can try "ostream << instanceOfNonTrivial". If it passes the compiler, then you know that operator << is defined for that class. So, no need to check the docs... just ask the compiler. :) [0] https://news.ycombinator.com/item?id=10775022 https://news.ycombinator.com/item?id=10775022
- GFK_of_xmaspast 11y ago> look at the docs to see if the ostream operator is implemented for the class Or give it a go and see if the ide/compiler complains.
- GFK_of_xmaspast 11y agoIn addition to the already-mentioned type safety and user-defined operator<< /etc overloading, it's very liberating to read into a std::string and not have to do a bunch of memory management and worry about null-termination or buffer overflows.
- Ace17 11y ago> How is iostream any better than *printf? printing user-defined types?