Enum classes should be preferred. The enum classes ("new enums", "strong enums") address three problems with traditional C++ enumerations:
- conventional enums implicitly convert to int, causing errors when someone does not want an enumeration to act as an integer. 2.conventional enums export their enumerators to the surrounding scope, causing name clashes.
- the underlying type of an enum cannot be specified, causing confusion, compatibility problems, and makes forward declaration impossible.
enum color
{
None = 0,
red = 1,
blue = 2,
green = 4,
All = red| blue | green,
};
enum fruit
{
apple=1,
melon=2,
orange=3,
};
enum class animals: unsigned char
{chicken, aligator, turtle};
enum class mamals
{cow, fox, dog};
in the main:
this is dangerous but it works:
std::cout<< (color::red==fruit::apple ? "equal":"not equal") <<std::endl;
this won't complie
std::cout<< (animals::chicken==mamals::cow ? "equal":"not equal") <<std::endl;
std::cout<< (animals::chicken==static_cast<animals>(0)? "equal":"not equal") <<std::endl;
your enum:
enum color
{
None = 0,
red = 1,
blue = 2,
green = 4,
All = red | blue | green,
};
iterator:
#include <type_traits>
template < typename C, C beginVal, C endVal>
class Iterator
{
typedef typename std::underlying_type<C>::type val_t;
int val;
public:
Iterator(const C & f) : val(static_cast<val_t>(f)) {}
Iterator() : val(static_cast<val_t>(beginVal)) {}
Iterator operator++()
{
++val;
return *this;
}
C operator*() { return static_cast<C>(val); }
Iterator begin() { return *this; } //default ctor is good
Iterator end()
{
static const Iterator endIter = ++Iterator(endVal); // cache it
return endIter;
}
bool operator!=(const Iterator& i) { return val != i.val; }
};
consumer of enum
void foo(color c)
{
std::cout <<c << std::endl;
}
now in you main
You'll need to specialize it
typedef Iterator<color, color::None, color::All> colorIterator;
for (color c : colorIterator())
{
foo(c);
}
Refs: 1
Automatically convert strongly typed enum into int
Refs: 1