iterating ENUM

I

Imran

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
 
B

Bob Hairgrove

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.
 
A

Ali Cehreli

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
 
B

Bob Hairgrove

Shared values introduce some complexity in your case, but the code
below is hopefully helpful for you.


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?
 
S

Siemel Naran

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?
 
H

Howard

Siemel Naran said:
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
 
A

Ali Cehreli

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.
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);
}

This line bothers me:

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
 
B

Bob Hairgrove

[snip]
The explicit construction of the enum is valid. Using 'value + 1'
alone would be an error.


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??
 
D

David Hilsee

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top