Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

range of enum

4 views
Skip to first unread message

srinivas reddy

unread,
Jul 4, 2003, 5:32:34 PM7/4/03
to
Hi,
What is the easiest way to calculate range of enum. Say I declare
enum e {min = -10, max = -5}; what would be the range?

I am using gcc3.01 compiler on SunOS2.8. When I compiled the following
program, it didn't return any error. The command I used is g++ -ansi
prog.cpp

int main()
{
enum e {min, max};

e f = e(9);
}

The book "The C++ programming language" by stroustrup says it is an
error as 9 is not in the range of e. What options should be provided
to g++ to enforece strict ANSI compliance. I would appreciate your
help.

tia,
Srinivas

John Harrison

unread,
Jul 4, 2003, 5:49:20 PM7/4/03
to

"srinivas reddy" <sriniva...@yahoo.com> wrote in message
news:ff8ef364.03070...@posting.google.com...

'It is an error' does not mean 'the compiler has to report an error'.

Stroustrup says that your program has undefined behaviour, he does not say
that it has a compile error. Some errors are your responsibility to find,
not the compilers.

john


Jakob Bieling

unread,
Jul 4, 2003, 6:07:39 PM7/4/03
to
"John Harrison" <john_an...@hotmail.com> wrote in message
news:be4sp1$1ckt8$1...@ID-196037.news.dfncis.de...

> "srinivas reddy" <sriniva...@yahoo.com> wrote in message
> news:ff8ef364.03070...@posting.google.com...

> > int main()


> > {
> > enum e {min, max};
> >
> > e f = e(9);
> > }

> > The book "The C++ programming language" by stroustrup says it is an
> > error as 9 is not in the range of e.

> Stroustrup says that your program has undefined behaviour

A previous thread in news://comp.lang.c++.moderated from May might be
worth reading:

http://groups.google.com/groups?threadm=runta.66941%24cO3.4526801%40bgtnsc04
-news.ops.worldnet.att.net

(or search for "overloaded enum operator" -with quotes!- in
news://comp.lang.c++.moderated)

Was of much help to me to understand what exactly is correct and what is
not.

regards
--
jb

(replace y with x if you want to reply by e-mail)


John Harrison

unread,
Jul 4, 2003, 6:15:57 PM7/4/03
to
>
> A previous thread in news://comp.lang.c++.moderated from May might be
> worth reading:
>
>
http://groups.google.com/groups?threadm=runta.66941%24cO3.4526801%40bgtnsc04
> -news.ops.worldnet.att.net
>
> (or search for "overloaded enum operator" -with quotes!- in
> news://comp.lang.c++.moderated)
>
> Was of much help to me to understand what exactly is correct and what
is
> not.
>

That just repeats what Stroustrup says doesn't it? If the value is outside
the smallest bit field capable of holding all of the values of the
enumeration then the behaviour is undefined, or did I miss something?

I think the OP's mistake is that he's understood 'the conversion is
undefined' (the phrase Stroustrup uses) as meaning the compiler will report
an error.

john


Jakob Bieling

unread,
Jul 4, 2003, 6:26:09 PM7/4/03
to
"John Harrison" <john_an...@hotmail.com> wrote in message
news:be4uau$1e3sh$1...@ID-196037.news.dfncis.de...

> >
> > A previous thread in news://comp.lang.c++.moderated from May might
be
> > worth reading:
> >
> >
>
http://groups.google.com/groups?threadm=runta.66941%24cO3.4526801%40bgtnsc04
> > -news.ops.worldnet.att.net
> >
> > (or search for "overloaded enum operator" -with quotes!- in
> > news://comp.lang.c++.moderated)
> >
> > Was of much help to me to understand what exactly is correct and
what
> is
> > not.
> >
>
> That just repeats what Stroustrup says doesn't it? If the value is outside
> the smallest bit field capable of holding all of the values of the
> enumeration then the behaviour is undefined, or did I miss something?

<nitpick> 'johnchx' states in the 5th message in that thread that the
behaviour is not undefined, but rather the value of the enum is unspecified
(7.2/9). </nitpick>

But, actually, I just thought that people who read this thread might
want to read the other thread as well. I should have posted this as a reply
to the OP really.

John Ericson

unread,
Jul 4, 2003, 8:16:48 PM7/4/03
to
"John Harrison" <john_an...@hotmail.com> wrote in
message news:be4sp1$1ckt8$1...@ID-196037.news.dfncis.de...

What's the problem? If you declare an enum with a minimum
element of -10, then AFAIK 9 would be in the range of enum
e.
--
Best Regards, JE

Thomas Matthews

unread,
Jul 5, 2003, 10:56:23 AM7/5/03
to

No. An enumerated type is more of a list of values, not
a range. Pascal and other languages have range types.
The C++ language doesn't.

The C++ language will use the smallest integral type to
contain the listed values. In the following:
enum {low = -23, high = 46};
There are two values. The compiler is allowed to use
two bits for storage and perhaps a lookup table. The
compiler implementers are allowed a wide range of
freedom to implement the enum.

To make an enum a range, you would have to specify each
value within the range:
enum {lowest = -1, l1 = 0, l2 = 1, l3 = 2}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

John Ericson

unread,
Jul 5, 2003, 12:13:00 PM7/5/03
to

"Thomas Matthews" <tomat...@sbcglobal.net> wrote in
message news:3F06E6B3...@sbcglobal.net...

> John Ericson wrote:
> > "John Harrison" <john_an...@hotmail.com> wrote in
> > message news:be4sp1$1ckt8$1...@ID-196037.news.dfncis.de...
> >
> >>"srinivas reddy" <sriniva...@yahoo.com> wrote in
> >
> > message
> >
> >>news:ff8ef364.03070...@posting.google.com...
> >>
> >>>Hi,
> >>>What is the easiest way to calculate range of enum. Say
> >>
> > I declare
> >
> >>>enum e {min = -10, max = -5}; what would be the range?
> >>>

<snip>

> >
> > What's the problem? If you declare an enum with a
minimum
> > element of -10, then AFAIK 9 would be in the range of
enum
> > e.
>
> No. An enumerated type is more of a list of values, not
> a range. Pascal and other languages have range types.
> The C++ language doesn't.
>
> The C++ language will use the smallest integral type to
> contain the listed values. In the following:
> enum {low = -23, high = 46};
> There are two values. The compiler is allowed to use
> two bits for storage and perhaps a lookup table. The
> compiler implementers are allowed a wide range of
> freedom to implement the enum.
>
> To make an enum a range, you would have to specify each
> value within the range:
> enum {lowest = -1, l1 = 0, l2 = 1, l3 = 2}
>
> --
> Thomas Matthews
>

<snip>

An enum has a set of values, type of the underlying type,
the values the minimum and maximum values that can be
represented by the smallest bitfield that can store the
minimum and maximum values of the enumerators in the enum
(see ISO/IEC 14882:1998(E) 7.2.6).

For an enum, say enum Fred{low = -23, high = 46}, the range
of Fred is I think [-64,+63]. With both enumerators
negative, I would think for the OP's example e {min = -10,
max = -5}; the range of e would be [-16,+15].

--
Best Regards, John E.


John Harrison

unread,
Jul 6, 2003, 9:19:25 AM7/6/03
to

"John Ericson" <jeri...@pacbell.net> wrote in message
news:QNoNa.223$i87.20...@newssvr14.news.prodigy.com...

Its a good point. But I think perhaps the OP just chose a bad example.

john


srinivas reddy

unread,
Jul 7, 2003, 12:22:14 PM7/7/03
to
Well, in the code I posted I didnt initialize min and max to -10 and
-5. So they would be initialized to 0 and 1 which makes 9 out of
range.

"John Harrison" <john_an...@hotmail.com> wrote in message news:<be97kt$2f29g$1...@ID-196037.news.dfncis.de>...

John Ericson

unread,
Jul 7, 2003, 11:47:15 PM7/7/03
to

"srinivas reddy" <sriniva...@yahoo.com> wrote in
message
news:ff8ef364.03070...@posting.google.com...
> Well, in the code I posted I didnt initialize min and max
to -10 and
> -5. So they would be initialized to 0 and 1 which makes 9
out of
> range.

<snip>

> > > > > What is the easiest way to calculate range of
enum. Say
> > I declare
> > > > > enum e {min = -10, max = -5}; what would be the
range?
> > > > >

<snip>

Yeah, although once you tell the compiler to shut up by
casting, you can't complain too much if the compiler doesn't
complain!

Best Regards, John E.


0 new messages