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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:00:01+00:00 2026-06-14T13:00:01+00:00

I was wondering recently what the requirements were for range based for loops in

  • 0

I was wondering recently what the requirements were for range based for loops in c++11, as I had only ever seen examples of intended usage:

for (auto person : people)
{
  cout << person.name << endl;
}

But given that a container need only have begin and end methods, but need not contain anything at all, would the below be considered ‘bad practice’ in any way? If nothing else, it is a fresh answer if someone asks you for a Fibonacci sequence in an interview!

#include <string>
#include <iostream>
#include <Windows.h>

using namespace std;

struct FibItr
{
    FibItr(int cur = 1, int prev = 0) : mCur(cur), mPrev(prev) {}
    FibItr & operator++()
    {
        mCur += mPrev;
        mPrev = mCur - mPrev;
        return *this;
    }

    int operator*(){ return mCur; }

    bool operator!=(const FibItr & _rhs)
    {
        return mCur != _rhs.mCur || mPrev != _rhs.mPrev;
    }   

    unsigned int mCur, mPrev;
};

struct Fib
{
    FibItr begin() { return FibItr(); }
    FibItr end() { return FibItr(0, 0); }
};

int main( int argc, char* argv[] ) 
{   
    for (auto num : Fib())
    {
        cout << num << endl;
        Sleep(500);
    }

    return 0;
}
  • 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-14T13:00:02+00:00Added an answer on June 14, 2026 at 1:00 pm

    The question is not really about the auto for-loop but if it is reasonable to implement stranger kind of iterators. While there are corner-cases you can make a perfect good argument for implementing some operations as iterators (memoized fibonacci being a good example).

    There are whole libraries devoted to turning iterators in something more, so some other people also think it is a good idea.

    As an aside: Implementing an iterator is tricky business, which is why methods like this should be used with care. Boost.Iterator is a good set of helpers that can make that easier.

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

Sidebar

Related Questions

I've had this behavior happen to me twice recently and I was wondering what
Recently I've been wondering about the fact that that an iOS app only has
I recently had a quick look at the new jQuery Sizzle, i was wondering
I recently started learning backbone and was wondering what would be the best way
I recently read a blog in Nodejitsu and I am wondering how this piece
I've recently been looking at Extreme Programming and wondering if it would be realistic
I'm wondering how I can display the recently created nodes by a user on
I have recently found myself becoming more negative about EF and cannot help wondering
I was wondering how to set up a database for storing actions people recently
I want to retrieve the most recently added record from CoreData. I was wondering

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.