By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

iterating ENUM

P: n/a
Hello All

I have a enum decla like this
enum Days // Declare enum type Days
{
saturday, // saturday = 0 by default
sunday = 0, // sunday = 0 as well
monday, // monday = 1
tuesday, // tuesday = 2
wednesday, // etc.
thursday,
friday
};

and am declaring a var

enum Days today = sunday;

Can I iterate enum Days, using any loops/ other methods

Thanks

Jul 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Wed, 11 Aug 2004 12:39:42 +0530, "Imran" <im******@in.bosch.com>
wrote:
Hello All

I have a enum decla like this
enum Days // Declare enum type Days
{
saturday, // saturday = 0 by default
sunday = 0, // sunday = 0 as well (suggestion: you can also write:
sunday = saturday
in case you ever want to change the value of saturday
but keep sunday in synch.) monday, // monday = 1
tuesday, // tuesday = 2
wednesday, // etc.
thursday,
friday
};

and am declaring a var

enum Days today = sunday;

Can I iterate enum Days, using any loops/ other methods


No, this is not possible. You could provide a global const vector or
array with all the distinct enum values, though. This is what I
usually do.

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #2

P: n/a
On Wed, 11 Aug 2004 12:39:42 +0530, Imran wrote:
I have a enum decla like this
enum Days // Declare enum type Days
{
saturday, // saturday = 0 by default
sunday = 0, // sunday = 0 as well
monday, // monday = 1
tuesday, // tuesday = 2
wednesday, // etc.
thursday,
friday
};
Shared values introduce some complexity in your case, but the code
below is hopefully helpful for you.
and am declaring a var

enum Days today = sunday;

Can I iterate enum Days, using any loops/ other methods


Stroustrup gives an iteration example for enums. This code is based on
that:

template <class Enum>
Enum & enum_increment(Enum & value, Enum begin, Enum end)
{
return value = (value == end) ? begin : Enum(value + 1);
}

enum Days
{
Days_begin,

monday = Days_begin,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday,

Days_end
};

Days & operator++ (Days & day)
{
return enum_increment(day, Days_begin, Days_end);
}

#include <iostream>

int main()
{
for (Days day = Days_begin; day != Days_end; ++day)
{
std::cout << day << '\n';
}
}

Ali

Jul 22 '05 #3

P: n/a
On Wed, 11 Aug 2004 01:56:58 -0700, Ali Cehreli <ac******@yahoo.com>
wrote:
On Wed, 11 Aug 2004 12:39:42 +0530, Imran wrote:
I have a enum decla like this
enum Days // Declare enum type Days
{
saturday, // saturday = 0 by default
sunday = 0, // sunday = 0 as well
monday, // monday = 1
tuesday, // tuesday = 2
wednesday, // etc.
thursday,
friday
};
Shared values introduce some complexity in your case, but the code
below is hopefully helpful for you.
and am declaring a var

enum Days today = sunday;

Can I iterate enum Days, using any loops/ other methods


Stroustrup gives an iteration example for enums. This code is based on
that:

template <class Enum>
Enum & enum_increment(Enum & value, Enum begin, Enum end)
{
return value = (value == end) ? begin : Enum(value + 1);
}

enum Days
{
Days_begin,

monday = Days_begin,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday,

Days_end
};

Days & operator++ (Days & day)
{
return enum_increment(day, Days_begin, Days_end);
}

#include <iostream>

int main()
{
for (Days day = Days_begin; day != Days_end; ++day)
{
std::cout << day << '\n';
}
}

Ali


Interesting ... is that in TC++PL? Never saw it before.

This line bothers me: return value = (value == end) ? begin : Enum(value + 1);


Enum(value + 1) ...
doesn't this give you a diagnostic about "initializing enum with int"?

What if value + 1 is not present in the enumeration?

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #4

P: n/a
"Ali Cehreli" <ac******@yahoo.com> wrote in message
On Wed, 11 Aug 2004 12:39:42 +0530, Imran wrote:
I have a enum decla like this
enum Days // Declare enum type Days
{
saturday, // saturday = 0 by default
sunday = 0, // sunday = 0 as well
monday, // monday = 1
tuesday, // tuesday = 2
wednesday, // etc.
thursday,
friday
};

template <class Enum>
Enum & enum_increment(Enum & value, Enum begin, Enum end)
{
return value = (value == end) ? begin : Enum(value + 1);
} Days & operator++ (Days & day)
{
return enum_increment(day, Days_begin, Days_end);
}


Good. But the problem is that in the original code, both Sunday and
Saturday map to zero. In that case I don't think there's any way to iterate
because a value of zero could mean either day, so the next day is either
Sunday or Monday, we don't know which.

So, can you think of other solutions?
Jul 22 '05 #5

P: n/a

"Siemel Naran" <Si*********@REMOVE.att.net> wrote in message
news:n8*********************@bgtnsc05-news.ops.worldnet.att.net...
"Ali Cehreli" <ac******@yahoo.com> wrote in message
On Wed, 11 Aug 2004 12:39:42 +0530, Imran wrote:
I have a enum decla like this
enum Days // Declare enum type Days
{
saturday, // saturday = 0 by default
sunday = 0, // sunday = 0 as well
monday, // monday = 1
tuesday, // tuesday = 2
wednesday, // etc.
thursday,
friday
};

template <class Enum>
Enum & enum_increment(Enum & value, Enum begin, Enum end)
{
return value = (value == end) ? begin : Enum(value + 1);
}

Days & operator++ (Days & day)
{
return enum_increment(day, Days_begin, Days_end);
}


Good. But the problem is that in the original code, both Sunday and
Saturday map to zero. In that case I don't think there's any way to

iterate because a value of zero could mean either day, so the next day is either
Sunday or Monday, we don't know which.

The next day would be Monday, because the next value is (0+1), which is 1,
which is Monday. So your loop would start at Saturday/Sunday (the same!),
followed by Monday, then Tuesday, ...
So, can you think of other solutions?


It depends on what you want to do.

If you want to loop over the enumeration, and have that loop include both
Saturday and Sunday as *different* loop values (i.e., you want them to be
handled in seperate passes through the loop), then it's simply not possible,
because you've defined them as the *same* value. Saturday and Sunday are
identical.

You could loop, using integers that start at Sunday (or Saturday) and end at
(after) Friday, but when that integer is 0, you won't be able to tell if
it's Saturday or Sunday, because it will simply be zero, which is valid for
both Saturday and Sunday. So that will work only if you can live with
treating day 0 as one day, ignoring whether it was intended to be Saturday
or Sunday. (Call it "Weekend"? :-))

If you need Saturday and Sunday to be handled in different passes through
the loop, then they absolutely *must* have different values. Perhaps you
could define a separate enumeration where they have different values?

One other idea: don't loop. You have only seven cases, right? Why not
handle them explicitly, instead of via a loop? So instead of something like

for (i = Saturday; i <= Friday; ++i)
DoOneDay(i);

write

DoSaturday();
DoSunday();
DoMonday();
DoTuesday();
etc.
-Howard

Jul 22 '05 #6

P: n/a
On Wed, 11 Aug 2004 03:58:42 -0700, Bob Hairgrove wrote:
On Wed, 11 Aug 2004 01:56:58 -0700, Ali Cehreli <ac******@yahoo.com>
wrote:
Can I iterate enum Days, using any loops/ other methods


Stroustrup gives an iteration example for enums. This code is based on
that:

template <class Enum>
Enum & enum_increment(Enum & value, Enum begin, Enum end) {
return value = (value == end) ? begin : Enum(value + 1);
}
I had used this utility template in a personal fun project before. I
used it for the implementation of operator++ of more than one enum
type.
enum Days
{
Days_begin,

monday = Days_begin,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday,

Days_end
};

Days & operator++ (Days & day)
{
return enum_increment(day, Days_begin, Days_end);
}

#include <iostream>

int main()
{
for (Days day = Days_begin; day != Days_end; ++day) {
std::cout << day << '\n';
}
}

Interesting ... is that in TC++PL? Never saw it before.
Yes, in TC++PL section 11.2.3, Operators and User-Defined Types.

He uses the days of the week as well :)

<quote>

enum Day { sun, mon, tue, wed, thu, fri, sat };

Day& operator++(Day& d)
{
return (d = (sat==d) ? sun : Day(d+1);
}

</quote>
This line bothers me:
return value = (value == end) ? begin : Enum(value + 1);
Enum(value + 1) ...
doesn't this give you a diagnostic about "initializing enum with
int"?


The explicit construction of the enum is valid. Using 'value + 1'
alone would be an error.
What if value + 1 is not present in the enumeration?


I think you missed the (value == end) check. Incrementing the last
value wraps the variable back to the beginning. This behavior is
arguable but makes the enum act like an int...

Ali
Jul 22 '05 #7

P: n/a
On Wed, 11 Aug 2004 11:29:11 -0700, Ali Cehreli <ac******@yahoo.com>
wrote:

[snip]
This line bothers me:
return value = (value == end) ? begin : Enum(value + 1);


Enum(value + 1) ...
doesn't this give you a diagnostic about "initializing enum with
int"?


The explicit construction of the enum is valid. Using 'value + 1'
alone would be an error.
What if value + 1 is not present in the enumeration?


I think you missed the (value == end) check. Incrementing the last
value wraps the variable back to the beginning. This behavior is
arguable but makes the enum act like an int...


But enums don't have to have contiguous values. This shouldn't work
if, for example, values 5 and 7 are present in an enum, and 6 is
missing ... or does "value+1" somehow evaluate to 7 when value==5??

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #8

P: n/a
"Bob Hairgrove" <in*****@bigfoot.com> wrote in message
news:n4********************************@4ax.com...
<snip>
But enums don't have to have contiguous values. This shouldn't work
if, for example, values 5 and 7 are present in an enum, and 6 is
missing ... or does "value+1" somehow evaluate to 7 when value==5??


The code was written with contiguous values in mind. It will not work for
non-contiguous values, and I don't know of any way that one could possibly
iterate over an enum with non-contiguous values without doing more work.

--
David Hilsee
Jul 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.