Topic: iosflags should be a type
Author: dietmar.kuehl@claas-solutions.de
Date: 1998/12/10 Raw View
Hi,
David Harmon (source@netcom.com) wrote:
> You know, it's a damn shame stuff like this has to happen. Didn't we
> learn anything from C? Why are the ios flags ints anyway? They ought
> to be a class type that implements the behaviour that is meaningful for
> them, and DOESN'T let you use things like operator|| where you meant
> operator| instead. What is the use of type safety when you make
> everything an int?
The standard makes no requirement whether 'ios_base::openmode' (this probably
what you are referring to) is a distinct type or just a typedef for an
integral type (or for an appropriate instantiation of 'bitset'). However,
even if 'openmode' is implemented eg. as an 'enum', this does not help much
for 'operator||()': An implicit conversion to a Boolean type is necessary in
some situations anyway, eg. to test whether a flag is set in an 'openmode'.
On the other hand, if there is no conversion from 'bool' to the corresponding
type (this is eg. the case for 'enum') then there should be a compiler error.
Thus, it is a quality of implementation issue to use a type different from
the built-in integral types (because there is a promotion from 'bool' to all
other integral types) and also different from 'bitset' (because there is
non-explicit constructor for 'bitset' taking an 'unsigned long'). My
implementation uses an 'enum' with corresponding overloaded bit operators.
The distinction between || and | is as fundamental in C++ as is the
distinction between == and =. If you mix these concepts up, you have a
problem anyway. However, maybe it would be nice if compilers could issue
appropriate warnings if it looks like a misuse. I guess, if you can come up
with a clear set of rules which correctly distinguishes between correct and
incorrect use for these operators in all common cases, most compiler writers
would gratefully implement this as a warning! The tricky thing is, however,
to find the correct rules... -- <mailto:dietmar.kuehl@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]