Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to elegantly put all enums into a std::set

Tags:

c++

enums

stdset

I have an enum and I want to put them all in the set( and then remove some with set_intersection algorithm, but that is offtopic). All works great except Im stuck on step 1. :)

If I have(real class has enum with higher cardinality)

class MyClass
{
enum Color{red, green , blue}
};

How would I init a std::set<MyClass::Color> to contain all enums.
I can obviously manually insert them one by one, do a for loop with casting since they are consecutive and start from 0 (I think that is required if I dont use = in enum definition), but Im looking for a more elegant way.

EDIT: I prefer C++03 solution if possible because current instance of problem requires it, but if not C++11 is good to know too.

like image 971
NoSenseEtAl Avatar asked Mar 14 '13 14:03

NoSenseEtAl


1 Answers

This is an option:

#define COLORS {red, green , blue}
enum Color COLORS;
static std::set<Color> color_set() {
    return COLORS;
}
#undef COLORS
like image 115
Pubby Avatar answered Sep 21 '22 19:09

Pubby