I have a recurrent chunk of code where I loop over all the members of an enum class
.
The for
loop that I currently use looks very unwieldly compared to the new range-based for
.
Is there any way to take advantage of new C++11 features to cut down on the verbosity for my current for
loop?
Current Code that I would like to improve:
enum class COLOR
{
Blue,
Red,
Green,
Purple,
First=Blue,
Last=Purple
};
inline COLOR operator++( COLOR& x ) { return x = (COLOR)(((int)(x) + 1)); }
int main(int argc, char** argv)
{
// any way to improve the next line with range-based for?
for( COLOR c=COLOR::First; c!=COLOR::Last; ++c )
{
// do work
}
return 0;
}
In other words, it would be nice if I could do something like:
for( const auto& c : COLOR )
{
// do work
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…