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 9162919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:19:41+00:00 2026-06-17T14:19:41+00:00

Lately I have been needing to implement small classes that generate a bunch of

  • 0

Lately I have been needing to implement small classes that generate a bunch of numbers. It would be very convenient if C++ had generators like python, but unfortunately it is not so.

So I have been thinking of how to best implement these types of objects, for easy iteration and composition. When we think of iterators over containers, they essentially only hold the index to an element, and the bulk of the information is in the container itself. This allows for several iterators to be referencing different elements within a collection at the same time.

When it come to state machines, it becomes apparent that the iterator will have to hold the whole state as several iterators need to be able to be independent. In that sense, the state machine class is more of a “builder” of these iterators which are the actual state machines.

As a toy example I have implemented the range generator (ala xrange in python), that can be used in loops:

// using range-for from c++11
for (auto i : range<int>(1, 30)) {
    cout << i << endl;
}

The code can be found on my bitbucket.

That said, it is awkward to store the whole state within the iterator, since the end() iterator is created just for the sake of comparing the ending state, which wastes space if the state is a large collection of members.

Has there been anything done with simple linear state machines, and looping over them with iterators?

  • 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-06-17T14:19:42+00:00Added an answer on June 17, 2026 at 2:19 pm

    If you support only forward iteration, you might be able to get away with using a different type for end() then for begin(). Here’s the basic idea

    class iterator;
    
    class iterator_end {
      typedef ... value_type;
      ...
    
      iterator& operator++ () { throw ... }
      value_type operator* () { throw ... }
      bool operator== (const iterator& e) const { return e == *this; }
    }
    
    class iterator {
      typedef ... value_type;
      ..
    
      iterator& operator++ () { ... }
      value_type operator* () { ... }
      bool operator== (const iterator_end& e) const { return are_we_done_yet }
    }
    
    class statemachine {
      iterator begin() const { ... }
      iterator_end end() const { ... }
    }
    

    I’ve never tries this though, so I can’t guarantee that this will work. Your state-machine’s iterator and const_iterator typedefs will specify a different type than end() returns, which may or may not cause trouble.

    Another possibility is to go with a variation on pimpl which uses boost::optional. Put the iterator state into a separate class, and store it within a boost::optional within the iterator. Leave the state unset for the iterator returned by end(). You won’t save any memory, but you avoid heap allocations (boost::optional doesn’t do any, it uses placement new!) and initialization overhead.

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

Sidebar

Related Questions

I have lately been developing a very simple app for iOS with PhoneGap. All
I am developing a project that lately have been taking off with increased popularity.
Lately I have been finding that some of the ColdFusion applications on my production
Lately when I have been looking at other code, I have noticed that I
Lately I have been playing around with intents and bundles. I thought I had
Lately I have been trying my hands on Eclipse IDE for java development. I
Lately I have been experiencing Eclipse Galileo (3.5) slowing down under Java 1.5b12 OR
Lately i have been wondering if there is a performance difference between repeating the
Lately I have been reading a lot of blog topics about big sites(facebook, twitter,
Lately I have been watching Pluralsight intro videos on MVC 3. I have never

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.