Also, I would mock you for using <iostream> at all but you've probably got legacy code just the same as I do.
Don't troll.
Type checking.
You should: https://news.ycombinator.com/item?id=10775022
It doesn't work with non-constexpr format strings, but there aren't many justifications for those.
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.
[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.
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. :)
Or give it a go and see if the ide/compiler complains.
Thanks for reminding me of this. Kudos!
That's not what's going on. Neither C nor C++ really permits that.
We're just talking about the relative merits of using printf and friends vs. using iostream to format data for the console or disk.
In either case, you have to write the code for your class that massages the class's internals correctly, but the question is, do you write to the ostream extraction and istream insertion API, or do you write to the substantially less well defined printf-and-friends API?
If you have a fundamental misunderstanding of the meaning of an operator, it's pretty easy to fail to correctly read a section of code.
Maybe you're someone who hates operator overloading. If so, that's a pity. It's a useful power tool. [0]
[0] Yes, all power tools can be abused. That conversation is tired and not worth revisiting.
void printStuff(ostream Os,
string WarningPart, string SizePart, string EndPart, int Size) {
//The English version might look like:
//"Warning: Size too big (200). Get smaller."
Os << WarningPart << SizePart << Size << EndPart << endl;
};
I expect that if I thought about the problem for a few minutes (and was unable to find any C++ i18n libraries) I would come up with something much nicer and easier to use than this thing I spent ten seconds working on. :)printf format strings are well thought-out. I was demonstrating that the same ideas can easily be used with io*stream.
:)
[0] http://www.boost.org/doc/libs/1_60_0/libs/locale/doc/html/in...
[1] http://www.boost.org/doc/libs/1_60_0/libs/locale/doc/html/he...
[2] http://www.boost.org/doc/libs/1_60_0/libs/locale/doc/html/me...
With printf, we have gettext. I don't really see how there's any analogy for gettext in iosream-world.
Funny thing. Boost more-or-less uses gettext, too! https://news.ycombinator.com/item?id=10774941
From the link: "Also note that Clang packaged in the Windows 64 NDK is actually 32-bit."