Re: int main(void) is better than int main()
On Jan 6, 11:16 pm, LR <lr...@superlink.net> wrote:
Tim H wrote:
[...]
I do wish that boost::format supported the * conversion.
printf("%3d: %#08x\n", x, y);
std::cout << boost::format("%3d: %#08x\n") %x %y;
How do you do this easily with plain old cout?
std::cout << std::setw( 3 ) << x
<< Gabi::HexFmt( 8, ios::showbase ) << y << '\n' ;
Except that I'd usually write:
std::cout << std::setw( 3 ) << x
<< "0x" << Gabi::HexFmt( 8, ios::uppercase ) << y <<
'\n' ;
WTH is Gabi? This also bolsters my point. In order to get simpler
output, you (a C++ expert, it seems) go outside the standard
implementation. And really - is what you wrote simpler or more
comprehensible than what I wrote?
I'm wondering if this Gabi:: is more typesafe than boost format?
Gabi is a namespace, and HexFmt is a manipulator, which works
more or less like std::setw(). HexFmt is also a poor example,
because it's really designed more to be an example of how to
write these sort of things. In an actual application, you's use
something similar, but which wouldn't require the ios::uppercase
argument---whether you output hex in upper or lower case would
be determined centrally, and not at each output site. (An
application specific manipulator could also output the "0x",
although it's not really very idiomatic for manipulators to
generate output.)
More recently, I've tended to use the decorator idiom more than
manipulators, since it's a lot more flexible---you could, for
example, output negative numbers as "(123)", instead of "-123".
(At my site, there's a ParsableString decorator, which can input
or output std::string, quoting it if the string contains
meta-characters, and stripping such quotes on input.)
and more direct than equivalent iostream manipulations for
all but the most trivial output.
I'm not sure that I agree with that either.
printf uses a language in a language. The language is very,
very terse (it was invented by people who programmed on real
tty's), and thus, not particularly readable. It contains a lot
of little known features.
At this point, at least for me, although I can't say that I'm
an expert in either, the C++ stream stuff all looks clearer to
me. I had to read up on %#08x very carefully and try it to
figure out the results.
And did I mention typesafety, again and again?
Or expandability.
And the advantage of having stringstreams, fstreams and
standard input, output and error all use the same interface?
printf, fprintf and sprintf all use the same language in a
language as well. The real advantage is when you start writing
your own streambuf's.
The fundamental difference between the two is the iostream is
framework, well adopted for customizing to the specific needs of
your application. printf et al. provides a small number of
fixed formats for a small number of types, and you can't go
beyond that. If you never have to output a user defined type,
never have to output to anything but a classical file, and it
doesn't matter if you constantly output the same semantic
information in different formats, depending on where it is
output, printf works fine. (As long as you never change any of
the data types in the program either, of course.)
--
James Kanze (GABI Software) mailto:james.kanze@gmail.com
Conseils en informatique orient=EF=BF=BDe objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=EF=BF=BDmard, 78210 St.-Cyr-l'=EF=BF=BDcole, France, +33 (0)1 30 2=
3 00 34