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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:39:54+00:00 2026-05-27T01:39:54+00:00

I am trying to test the example of checked iterator from Stroustrup’s C++ book.

  • 0

I am trying to test the example of “checked iterator” from Stroustrup’s C++ book. The code has a run time error: “list iterators incompatible”.

The error is trigger by “if( c->end() == p )” of function “valid()“. What does this error mean? I guess both c->end() and p should be of type list<int>::iterator.

In addition, I don’t understand why “operator” was implemented in that way. BTW, I am using VS2008.

struct out_of_bounds{
    out_of_bounds() {}
};

template<class Cont, class Iter = typename Cont::iterator>
class Checked_iter : public iterator_traits<Iter> // deriving from iterator_traits
{
    Iter curr;   // iterator for current position
    Cont* c;    // pointer to current container
    // ...
public:

    void valid(Iter p){
        if ( c->end() == p )
            return;
        for (Iter pp = c->begin(); pp != c->end(); ++pp ){
            if ( pp == p )
                return;
        }
        throw out_of_bounds();
    }

    friend bool operator==(const Checked_iter& i, const Checked_iter& j){
        return i.c == j.c && i.curr == j.curr;
    }

    // no default initializer
    // use default copy constructor and copy assignment
    Checked_iter(Cont x, Iter p) : c(&x), curr(p) { valid(p); }

    reference operator*(){
        if ( curr == c->end() ) throw out_of_bounds();
        return *curr;
    }

    pointer operator->(){
        return &*curr; // checked by *
    }

    Checked_iter operator+(difference_type d){ // for random-access iterator only
        if ( c->end() - curr <= d  )
            throw out_of_bounds();
        return Checked_iter(c, curr+d);
    }

    reference operator[](difference_type d){ // for random-access iterator only
        if ( c->end() - curr <= d  ) throw out_of_bounds();
        return c[d]; 
    }

    Checked_iter& operator++(){ // prefix ++
        if ( curr == c->end() ) throw out_of_bounds();
        ++curr;
        return *this;
    }

    Checked_iter& operator++(int) { // postfix ++
        Checked_iter temp = *this;
        ++*this; // checked by prefix ++
        return temp;
    }

    Checked_iter& operator--() { // prefix --
        if ( curr == c->begin() ) throw out_of_bounds();
        --curr;
        return *this;
    }

    Checked_iter& operator--(int) { // postfix --
        Checked_iter temp = *this;
        --*this;  // check by prefix
        return temp;
    }

    difference_type index() { return curr-c.begin(); } // random-access only
    Iter unchecked(){ return curr; }
};

void f2(list<int>& ls){

    int count = 0;
    try{
        Checked_iter< list<int> > p(ls, ls.begin() );
        while(true){
            ++p;
            ++count;
            //cout << "element: " << *p << "   count: " << count << endl;
        }
    }
    catch(out_of_bounds){
        cout << "overrun after " << count << " tries \n";
    }

}

void test9(){
    int a[] = {0,1,2,3,4,5,6,7,8,9};
    list<int> l(a, a+sizeof(a)/sizeof(int));
    show(l);
    f2(l);
}
  • 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-27T01:39:55+00:00Added an answer on May 27, 2026 at 1:39 am

    The constructor for Checked_iter should take Cont&, not Cont. You are storing a pointer to a temporary copy.

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

Sidebar

Related Questions

I am trying to test an example of search() from Stroustup's book. string quote(why
I'm trying to test cherrypy framework by using example from their site: import cherrypy
I am trying to test whether certain time has elapsed. I use javascript. Both
im trying to test example from developers webpage $token_url = https://graph.facebook.com/oauth/access_token? . client_id= .
I,m trying to work through and test a Voice Recognition example based on the
I'm trying to implement a WebLogic job scheduling example, to test my cluster capabilities
just trying to test for equality in this piece of code, but getting a
I am trying to test the likelihood that a particular clustering of data has
I am trying to test that a particular method throws an expected exception from
I'm trying to test some python code that uses urllib2 and lxml. I've seen

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.