允许基于范围的枚举类? [英] Allow for Range-Based For with enum classes?

查看:118
本文介绍了允许基于范围的枚举类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环的代码块,我循环了一个枚举类的所有成员



for 循环,我目前使用的看起来相当于新的范围为



有没有办法利用新的C ++ 11功能来减少目前循环的的详细程度? / p>

我想改进的当前代码:

 枚举类COLOR 
{
蓝色,
红色,
绿色,
紫色,
首先=蓝色,
最后=紫色
};

inline COLOR运算符++(COLOR& x){return x =(COLOR)(((int)(x)+ 1)); }

int main(int argc,char ** argv)
{
//任何改进下一行的基于范围的方法?
(COLOR c = COLOR :: First; c!= COLOR :: Last; ++ c)
{
//做工作
}
返回0 ;
}

换句话说,如果我可以做一些像/ b>

  for(const auto& c:COLOR)
{
// do work
}


解决方案

将枚举本身迭代为迭代器是枚举不好的想法,我建议使用一个实际的迭代器,如在deft_code的答案。但是,如果这真的是你想要的:

  COLOR operator ++(COLOR& x){return x =(COLOR)(std: :underlying_type< COLOR> :: type(x)+ 1); 
COLOR运算符*(COLOR c){return c;}
COLOR begin(COLOR r){return COLOR :: First;}
颜色结束(COLOR r){COLOR l = COLOR ::持续; return l ++;}

int main(){
for(const auto& c:COLOR()){//注意我在这里添加了一个实例
//做工作
}
return 0;
}

在这里工作: http://ideone.com/cyTGD8



在迭代器方面,最简单的方法是简单的:

  extern const COLOR COLORS [(int)COLOR :: Last + 1]; 
const COLOR COLORS [] = {COLOR :: Blue,COLOR :: Red,COLOR :: Green,COLOR :: Purple};

int main(){
for(const auto& c:COLOR()){//注意我在这里添加了一个实例
//做工作
}
return 0;
}

如下所示: http://ideone.com/9XadVt



(数组的单独声明和定义使其成为编译器错误,如果颜色数量不匹配数组中的元素数量。优秀的安全检查。)


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
}

解决方案

Iterating enumerations with the enumeration itself as an iterator is a poor idea, and I recommend using an actual iterator as in deft_code's answer. But if this is really what you want:

COLOR operator++(COLOR& x) { return x = (COLOR)(std::underlying_type<COLOR>::type(x) + 1); }
COLOR operator*(COLOR c) {return c;} 
COLOR begin(COLOR r) {return COLOR::First;}
COLOR end(COLOR r)   {COLOR l=COLOR::Last; return l++;}

int main() { 
    for(const auto& c : COLOR()) { //note I added parenthesis here to make an instance
        //do work
    }
    return 0;
}

Working here: http://ideone.com/cyTGD8


On the iterator side of things, the easiest way is simply:

extern const COLOR COLORS[(int)COLOR::Last+1];
const COLOR COLORS[] = {COLOR::Blue, COLOR::Red, COLOR::Green, COLOR::Purple};

int main() { 
    for(const auto& c : COLOR()) { //note I added parenthesis here to make an instance
        //do work
    }
    return 0;
}

As seen here: http://ideone.com/9XadVt

(The separate declaration and defintinion of the array makes it a compiler error if the number of colors doesn't match the number of elements in the array. Excellent easy safety check.)

这篇关于允许基于范围的枚举类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆