Topic: Proposal for the missing "general" flag in ios_base


Author: dsp@bdal.de (Daniel Spangenberg)
Date: Mon, 24 Feb 2003 20:10:30 +0000 (UTC)
Raw View
Hello,
I would like to make a proposal concerning a missing "general" flag of
the iso_base::fmtflags family as well as a missing "general"
manipulator.

OVERVIEW
ios_base provides some basic flags for the stream format
(ios_base::fmtflags), e.g. scientific, fixed. Some of them belong to
different categories (floatfield, adjustfield). Most of these flags have
a corresponding manipulator for short-cut streamings like

std::cout << std::fixed << std::setprecision(2) << 3.141516 <<
std::endl;

The existence of both these flags and these manipulators reflect the
need for a general way to change the stream behaviour.

MOTIVATION
It is noticeable, that also there are three fundamental formating styles
for floating-point numbers (general, fixed, exponential) inherited form
long existing C practice, only two of same have a corresponding named
format flag (ios_base::fixed, ios_base::exponential) and only two of
them have corresponding manipulators (w/o arguments), namely std::fixed
and std::exponential.

The standard provides no special method, to realize something like a
"general" format. The C++98 standard mentions, that the floatfield can
take TWO possible values, namely

scientific | fixed

Wellknown authorities, like Stroustrup in "The C++ programming
language", mention the possibility to change the floating-point format
via the route

ios_base stream;
stream.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);

to enforce the general style, although I did not find anything in Our
current Standard, which explicitely guarentees the success of this
method. Assuming, that it IS explicitely allowed, this is a very ugly
and unreadable method to change the formating behaviour of
floating-point numbers either.
It also contradicts to other fundamental flags (like the stream state
flags), where corresponding 0 flags (like std::ios_base::good, see
27.4.2.1.3/2) have a named specifier.

Given, that the above mentioned method works, there also exists no
readable/symmetric method, to realize the same result by means of the
standard manipulators. The shortest way, I can imagine, would be
something like

stream << resetiosflags(std::ios_base::floatfield);

assuming, that the above assumptions concerning the 0 value holds.
Note, that this behaviour is also not symmetric to other triple-state
flags (like adjustfield), which provide all three named flags as well as
three manipulators (like internal(), right(), and left())

PROPOSED RESULUTION:

Add a further predefined std::ios_base::fmtflags member as part of the
std::ios_base class in listing 27.4.2, namely

static const fmtflags general;

Further on, table 83 should be extended:

general generates floatingpoint output in general notation;

AND fix the last entry of table 84 to read as

floatfield general | scientific | fixed

OR add a further paragraph similar to 27.4.2.1.3/2:

Type iostate also defines the constant:
=97 general, the value zero.

Personally I prefer the first solution.

Further on, we could add a further manipulator in 27.4.5.4 "floatfield
manipulators", namely:

ios_base& general(ios_base& str);
Effects: Calls str.setf(ios_base::general, ios_base::floatfield).
OR
Effects: Calls str.setf(ios_base(0), ios_base::floatfield).

Returns: str.

Last but not least, "Table 58=97Floatingpoint conversions" could be
adapted (also this is not necessary)

Neither of the proposed extensions should conflict with user-defined
codes.

Thank you for your attendance,

Daniel Spangenberg



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]