You must be bored. ;)
> "Folks seem to enjoy pointing out that I use printf in many of my
> examples of "modern C++", as if printf is not really proper C++.
Since C++ does not have anything useful in standard set the 'printf'
is as perfect for usage in illustration code of raw C++ as is 'cout'.
> Apparently, I should be using cout.
It is sort of like discussing how good 'malloc' is compared to 'new'
when we can use neither in actual C++ code (unless we write standard
library or something competing with standard library) and it seems that
we only gain from avoiding those.
> The trouble is that cout isn't exactly modern either. It has been
> around for as long as I can remember and it certainly doesn't
> exemplify modern C++ as envisioned by C++11 and beyond.
Yeah, pot vs kettle.
> The oldest C++ textbook on my shelf was printed in 1993 and covers
> cout. I even posed the question to some C++ historians and they were
> able to date it back as far as 1989. Therefore, the argument that
> printf is old and cout is modern doesn't fly.
Indeed, poop vs feces here.
> A truly modern C++ solution would also not be substantially
> slower than hand-written code. Most printf implementations today
> provide adequate type checking both at compile time and run time.
> Visual C++ even provides secure versions that make it quite
> straightforward to write defensive code quite easily with printf. Go
> ahead and use cout if you prefer, but don't claim it's the modern
> replacement for printf."
I don't understand. Both are dead without resurrection possible. Let
me evaluate ...
'printf' format becomes quickly complex and cryptic when the format
specifiers are something else and more detailed than the '%d' and '%s'.
Scott Lurndal already beat me in posting his oh so readable examples
else-thread. The format specifiers don't specify everything; we need
things like 'strftime' just to output time etc. also somewhere
underneath a locale is screwing things over. However the 'cout' I/O
manipulators are additionally annoyingly verbose so Scott is right
that we get COBOL trying to format raw numbers using those. 'cout' is
no way more familiar with 'struct tm' than 'printf' so sorry but
no sugar and it is screwed up by locale as deep.
Oh. But we next to never have raw context-free numbers exposed in C++.
So we run to extend 'cout' by overloading 'operator<<(ostream,Foo const&)'
*Terrible* idea. Especially devastating with novice in charge plus
dynamic or static polymorphism and/or any forms of implicit
conversions. They stare and debug it for hours and only positive outcome
is that learn to hate 'cout' with passion.
Soon we then pick the bit cleaner way out of it by making everywhere
things like 'Foo.asText(optional_specifiers)'. As ultimate result we
cut most features of both 'printf' and 'cout'. We use only '%s'
with 'printf' and only 'operator<<(ostream,string const&)' with 'cout'.
Hmm. What we now got? Big pile of unneeded bloat features underneath
dragging down our application's efficiency on *both* cases. And since
we are down to strings only it is now simple to verify that even
'operator+' of 'std::string' beats them both. :D
> I have tried to cut my printf usage to very basic functions that
> get called a lot so they are fairly well tested. Printf usege in
> an error message is a bad idea, IMHO.
There are no reason to use these in human interface when there is
anything more comfortable (QString, Boost.Format, your own lib,
whatever) available (and that must be is always the case).
There are no point to use these for structured text (like XML,
JSON, whatever). No well-performing parser/generator can result.
Only two usages I can see: For constructing cryptic code to ensure
job security for ourselves and for usage in academic raw C++
example codes for to screw schoolboys and enforce them to think
about ways out of that excrement.