3 ms·
With 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 ost
by ambrice 11y ago
With 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.
- simoncion 11y agoIt has been so long that that bit of information had dropped out of my brain. I went to verify, and -obviously- there is no auto-conversion for instances of a non-trivial class. Thanks for reminding me of this. Kudos!