Topic: Inheriting from an enum
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/08/23 Raw View
in article <7p9n6t$115$1@engnews1.eng.sun.com>, Steve Clamage
<clamage@eng.sun.com> writes
>Suppose on an 8-bit byte-addressed system we define
> enum Y { a=0, b=32767 };
>I don't see a requirement in the C standard that Y take up 16 bits.
>I think an implementation could use an 8-bit byte to store values
>of the enum, and generate code (for example) to convert non-zero
>values to 32767 when the variable is read, and truncate any stored
>value to 8 bits.
After a few email exchanges between various C experts to refine my
understanding of the requirements in C for enumerated types the
following is as close to a definitive answer as I can get without a
formal request for interpretation.
> Simply is a conforming implementation required to support:
>
> enum X {low=INT_MIN, high = INT_MAX};
Yes, it is.
The type enum X will be compatible with some integer type that has at
least
that range. [It could be int, long, or, in C9X long long, or it could be
some extended integer type.]
> and in doing so require that all values in the range [low, high] have a
> distinct representation?
Yes. For any enumerated type, all values in the range [low, high] have a
distinct representation, no matter what the values of low and high are.
> Where does the 12 bits come in? Because ints
> must be at least 16 bits.
C9X widens the requirement that C89 had, to require a minimum of 12 bits
in the integer type selected (this could be a char type if it is
sufficiently wide)
enum Y { low = -127, high = 127 };
could be compatible with signed char.
enum Z { low = -2047, high = 2047 };
could be compatible with the extended integer type int_least12_t.
This confirms my prior belief that C++ actually places a lower
requirement on the range than does C.
I also think that the requirements for C and C++ differ with regard to
symmetry.
C certainly requires that
enum Unbalanced {low = -1, high = INT_MAX};
support the entire range of int values (because C requires there to be
an underlying integer type, and this must be as symmetrical as possible
- 2s complement does not support exact symmetry)
OTOH I can find no equivalent requirement in C++ (maybe I am not looking
in the right places).
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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 ]