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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:38:44+00:00 2026-05-28T23:38:44+00:00

A part of the source code for a project I’m working on, which is

  • 0

A part of the source code for a project I’m working on, which is responsible for compressing a sequence of ‘events’, looks like this:

#include <iterator>
#include <list>

typedef int Event;

typedef std::list<Event> EventList;

struct Compressor {
    // Returns an iterator behind the last element which was 'eaten'
    virtual EventList::const_iterator eatEvents( const EventList &l ) = 0;
};

// Plenty of Compressor subclasses exist

void compressAndCopyEatenEvents( Compressor &c ) {
    EventList e;
    e.push_back( 1 );
    EventList::const_iterator newEnd = c.eatEvents( e );

    EventList eatenEvents;
    std::copy( e.begin(), newEnd, std::back_inserter( eatenEvents ) ); // barfs
}

The issue here is that the compressAndCopyEatenEvents function has a non-const list of events; this list os passed to the eatEvents methods, which takes a reference-to-const and yields a const_iterator. Now the compressAndCopyEatenEvenst function would like to copy the range of eaten events away, so it decides to use some algorithm (std::copy here, which of course could just as well be replaced with the right std::list constructor call – the point is that this problem exists with all kinds of ranges).

Unfortunately(?) many (if not all?) ranges need to be composed from the same iterator type. However, in the above code, ‘e.begin()’ yields an EventList::iterator (because the object is not const) but ‘newEnd’ is an EventList::const_iterator.

Is there a design weakness here which causes this mess? How would you tackle it?

  • 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-28T23:38:45+00:00Added an answer on May 28, 2026 at 11:38 pm

    In C++03 the only possible way is to cast. (which is ugly, which is a design flaw, yes).

    std::copy( static_cast<EventList::const_iterator>e.begin(), newEnd, std::back_inserter( eatenEvents ) );
    

    or have another named variable:

    EventList::const_iterator constBegin = e.begin();
    std::copy(constBegin , newEnd, std::back_inserter( eatenEvents ) );
    

    In C++11 you have cbegin and cend functions (that always return const_iterators) so you’d do simply

    std::copy( e.cbegin(), newEnd, std::back_inserter( eatenEvents ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working in VS2005, I have a part of freeimage source code. I
I'm looking at the source code for Tor as part of an RA project.
I am working on an enormous project (the project) which is open-source, and I
I'm working on an open source project I'm part of called jQuery. I'm trying
I have the source code for a C# project (which wasn't written by me)
I'm trying to read the Prototype source. I've come to this part.(Unfortunately, this snippet
As part of my Final Year Project I've developed a desktop application, which fits
This is a part of my project, my project basically uses a motion sensor
I am working on a project where I need to crunch large integers (like
I've attached the Android source code via File > Project Structure > Modules >

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.