Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6927865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:06:37+00:00 2026-05-27T11:06:37+00:00

I have a recurrent chunk of code where I loop over all the members

  • 0

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:06:38+00:00Added an answer on May 27, 2026 at 11:06 am

    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() { 
        //note the parenthesis after COLOR to make an instance
        for(const auto& c : COLOR()) {
            //do work
        }
        return 0;
    }
    

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


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

    const COLOR COLORS[] = {COLOR::Blue, COLOR::Red, COLOR::Green, COLOR::Purple};
    const COLOR (&COLORREF)[(int)COLOR::Last+1] = COLORS;
    
    int main() { 
        for(const auto& c : COLORS) {
            //do work
        }
        return 0;
    }
    

    As seen here: http://coliru.stacked-crooked.com/a/5d356cc91556d6ef

    (The separate defintinion and the reference 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.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we have a frequently recurring lines in the code that check for the return
Here's a recurrent problem that I have and I think maybe CDI events can
Can I use another bar application that ApplicationBar of WP7? I would have a
I have a recurrent design issue when I use sliders in my photo galleries.
I have a tabular file with a column that is recurrent example toto tata
Suppose I have a non-recurring event that needs to be raised X seconds from
Have you guys had any experiences (positive or negative) by placing your source code/solution
I have an entity with three fields Task { Active BOOL Recurrent Int Started
I have a bit of experience setting up online payment systems that accept credit
The Sitecore Calendar Module doesn't have the features that we want: recurring events, multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.