In comp.lang.c++ olcott <
No...@nowhere.com> wrote:
> I still (after decades of C++) mostly use
> <stdio.h> for IO. The C++ formatted output is simply too tedious.
One advantage of the C++ style streams is that they are easily extensible
to support custom types while retaining the same usage syntax. I find this
very handy especially for things like debug output and such.
In other words, if I have something like:
class MyClass { ... };
I can then implement an std::ostream operator<< overload that allows me to
write things like:
MyClass obj = whatever;
std::cout << "a=" << a << ", b=" << b << ", obj=" << obj << "\n";
Sometimes I prefer implementing a separate debug printing function as a
variadic template, which I can call like:
dPrint("a=", a, ", b=", b, ", obj=", obj, "\n");
but even then the extensibility of std::ostream becomes handy because it
simplifies the implementation of that dPrint() function (because its
implementation can simply pass the parameters to std::ostream::operator<<).
Doing either one of those two things using <cstdio> only, instead of using
<iostream>, would be a lot more difficult. That's because of the lack of
overloads (and type safety) in <cstdio>.
That being said, there are other situations where std::printf() is a lot
handier than std::cout. For example, compare:
std::printf("%08X", value);
to the equivalent:
// Need this because some flag changes are persistent:
std::ios_base::fmtflags f = std::cout.flags();
std::cout << std::hex << std::uppercase << std::setw(8)
<< std::setfill('0') << value;
std::cout.flags(f);
which is, admittedly, a bit ridiculous.