From: Stewart, Robert (stewart_at_[hidden])
Date: 2002-02-01 11:49:57
From: Karl Nelson [SMTP:kenelson_at_[hidden]]
> > From: Karl Nelson [SMTP:kenelson_at_[hidden]]
> > >
> > > Does cout << format("%#010x" , complex<int>(20,21) ); mean
> > > A) the user wants all integers output to be show base with
> > > 12 charactors zero padded. 0x00000016+0x00000017i
> > > B) The total width should be 10 with showbase and zero padding?
> > > 0x16+0x17i
> > I would definitely expect B. Otherwise, the width has relatively little
> > control over the output; any user-defined type could wreak havoc on
> > formatting.
> The problem is you just interpreted "width" to mean "maximum width".
> But in this context it means "minumum width". As it does in iostream
> as well. In fact, iostream would completely ignore width for
> the above specifier.
Yes and no. When I said a "user-defined type could wreak havoc on
formatting," I was thinking, in part, in terms of maximum width. I think
there's a place for that. However, I do realize that width for both
printf() and IOStreams is minimum, and there's good reason for that: you
don't accidentally lose valuable information.
Still, "0x16+0x17i" is only 10 characters, so output B is what I would
expect to be the result.
> #include <iostream>
> #include <iomanip>
> #include <complex>
> using namespace std;
> int main()
> for (int i=1;i<1000000;i*=10)
> cout << setw(5) << i << " ";
> cout << setw(5) << complex<int>(i) << endl;
> 1 (1,0)
> 10 (10,0)
> 100 (100,0)
> 1000 (1000,0)
> 10000 (10000,0)
> 100000 (100000,0)
> Thus either A or B would have been better.
Since your width was only five, you never got to see any padding with the
complex<> output. cout was using setw(5) as indicating a minimum width, but
the result was always at least five characters.
> This is a big lump in printf formating (which we should not replicate)
> which is the width field means generate no less than. According to that
> rule I would guess output A. It generated no less than 10 chars for
> each int conversion.
That's not such a problem as you suggest. If printf() (and IOStreams) were
to treat that as a maximum width, then the output would be truncated without
any indication. They err on the side of showing all important information
at the cost of mucking with any formatting you may have desired.
> Second point, is that unlike fortran C++ does not have a cap limit
> when you hit the maximum. Ie. should format("%.4d" , 12345) print
> 1234, 2345, or ####?
Displaying some placeholder characters in lieu of output when a maximum
width specification is exceeded is reasonable. However, the default should
be a minimum width approach; the format string should have to invoke the
maximum width behavior explicitly. So, in IOStreams, there should be a
setmaxw() manipulator with corresponding mfs and state information. There
should also be a locale-controlled fill character to indicate data that
would have displayed were it not wider than the allotted field width.
Susquehanna International Group, LLP
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk